Skip to content

Commit ea154c6

Browse files
committed
Commiting files for 08/06
1 parent bb21861 commit ea154c6

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Feel free to add issues, comment and pull request.
2929
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
3030
| Leetcode | [557. Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Java](./java/reverseWords.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
3131
| Leetcode | [387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Java](./java/FirstUniqChar.java) \| [Python](./Python/) | _O(n)_ | _O(n)_ | Easy | |
32+
| Leetcode | [647. Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [Java](./java/CountSubstrings.java) \| [Python](./Python/) | _O(n^2)_ | _O(1)_ | Easy | |
3233

3334
## HashMap
3435
| Website | Title | Solution | Time | Space | Difficulty | Note|
@@ -42,3 +43,9 @@ Feel free to add issues, comment and pull request.
4243
| Leetcode | [152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Java](./java/MaximumProductSubArray.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Medium | |
4344
| Leetcode | [628. Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers) | [Java](./java/MaximumProduct.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
4445
| Leetcode | [62. Unique Paths](https://leetcode.com/problems/unique-paths/) | [Java](./java/UniquePaths.java) \| [Python](./Python/) | _O(m*n)_ | _O(m+n)_ | Easy | |
46+
47+
48+
## Recursion
49+
| Website | Title | Solution | Time | Space | Difficulty | Note|
50+
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
51+
| Leetcode | [654. Maximum Binary Tree](https://leetcode.com/contest/leetcode-weekly-contest-44/problems/maximum-binary-tree/) | [Java](./java/654. Maximum Binary Tree) \| [Python](./Python/) | _O(n log n)_ | _O(n)_ | Easy | |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public TreeNode constructMaximumBinaryTree(int[] nums)
12+
{
13+
//Recursive helper function
14+
return helper(nums,0,nums.length-1);
15+
}
16+
17+
public TreeNode helper(int [] nums, int low, int high)
18+
{
19+
if(high < low)
20+
return null;
21+
22+
//Identify the maximum element's index
23+
int maxIndex = getMaxIndex(nums, low, high);
24+
25+
//Construct the root node
26+
TreeNode root = new TreeNode(nums[maxIndex]);
27+
28+
//Generate the left sub tree from the left part of sub array
29+
root.left = helper(nums,low, maxIndex-1);
30+
31+
//Generate the right sub tree from the right part of sub array
32+
root.right = helper(nums, maxIndex+1, high);
33+
34+
//Return the root
35+
return root;
36+
}
37+
38+
public int getMaxIndex(int [] nums, int low, int high)
39+
{
40+
//Identify the maximum element's index to construct the root element
41+
int maxIndex = low;
42+
for(int i = low+1; i <= high; i++)
43+
{
44+
if(nums[i] > nums[maxIndex])
45+
maxIndex = i;
46+
}
47+
return maxIndex;
48+
}
49+
}

java/CountSubstrings.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution
2+
{
3+
// This problem is exactly similar to longest palindromic substring. Just need to add count++.
4+
// Generate all the substrings of a string and expand around the centre to see if it formas a palindrome
5+
private int count = 0;
6+
7+
public int countSubstrings(String s)
8+
{
9+
for(int i=0; i<s.length(); i++)
10+
{
11+
isPalindrome(i,i,s); //Even length
12+
isPalindrome(i,i+1,s); //Odd length
13+
}
14+
return count;
15+
}
16+
17+
public void isPalindrome(int left, int right, String s)
18+
{
19+
while(left >=0 && right < s.length() && s.charAt(left) == s.charAt(right))
20+
{
21+
count++; left--; right++;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)