Skip to content

Commit 28fa287

Browse files
committed
Added 1 solution & refactored 2 solutions
1 parent 1b13a38 commit 28fa287

File tree

3 files changed

+93
-63
lines changed

3 files changed

+93
-63
lines changed
Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
class Solution {
2-
public List<Integer> findSubstring(String s, String[] words) {
3-
List<Integer> indexes = new ArrayList<>();
4-
if (words.length == 0) {
5-
return indexes;
6-
}
7-
8-
Map<String, Integer> freqMap = new HashMap<>();
9-
for (String word : words) {
10-
freqMap.put(word, freqMap.getOrDefault(word, 0) + 1);
11-
}
12-
13-
int start = 0;
14-
int oneWordLen = words[0].length();
15-
int totalLen = oneWordLen * words.length;
16-
17-
while ((s.length() - start) >= totalLen) {
18-
String subStr = s.substring(start, start + totalLen);
19-
int i = 0;
20-
Map<String, Integer> map = new HashMap<>(freqMap);
21-
while (i < subStr.length()) {
22-
String wordStr = subStr.substring(i, i + oneWordLen);
23-
24-
if (!map.containsKey(wordStr) || map.get(wordStr) < 1) {
25-
break;
26-
}
27-
28-
map.put(wordStr, map.get(wordStr) - 1);
29-
i += oneWordLen;
30-
}
31-
32-
if (i >= subStr.length()) {
33-
indexes.add(start);
34-
}
35-
36-
start++;
37-
}
38-
39-
return indexes;
2+
public List<Integer> findSubstring(String s, String[] words) {
3+
if (s.length() == 0 || words.length == 0) {
4+
return new ArrayList<>();
405
}
6+
Map<String, Integer> wordFreqMap = new HashMap<>();
7+
for (String word : words) {
8+
wordFreqMap.put(word, wordFreqMap.getOrDefault(word, 0) + 1);
9+
}
10+
int stringLength = s.length();
11+
int wordCount = words.length;
12+
int singleWordLength = words[0].length();
13+
List<Integer> indices = new ArrayList<>();
14+
for (int i = 0; i + singleWordLength * wordCount <= stringLength; i++) {
15+
if (match(s, i, singleWordLength, wordFreqMap, wordCount)) {
16+
indices.add(i);
17+
}
18+
}
19+
return indices;
20+
}
21+
22+
private boolean match(String s, int start, int singleWordLength, Map<String, Integer> wordFreqMap, int wordCount) {
23+
Map<String, Integer> currFreqMap = new HashMap<>();
24+
for (int i = 0; i < wordCount; i++) {
25+
String currWord = s.substring(start + i * singleWordLength, start + (i + 1) * singleWordLength);
26+
Integer freq = wordFreqMap.get(currWord);
27+
// Word not in required words
28+
if (freq == null) {
29+
return false;
30+
}
31+
currFreqMap.put(currWord, currFreqMap.getOrDefault(currWord, 0) + 1);
32+
// Word occurs more than the required count
33+
if (currFreqMap.get(currWord) > freq) {
34+
return false;
35+
}
36+
}
37+
return true;
38+
}
4139
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
class Solution {
11+
int idx;
12+
public TreeNode bstFromPreorder(int[] preorder) {
13+
idx = 0;
14+
return helper(preorder, Integer.MIN_VALUE, Integer.MAX_VALUE);
15+
}
16+
17+
private TreeNode helper(int[] preorder, int lower, int upper) {
18+
if (idx == preorder.length || preorder[idx] < lower || preorder[idx] > upper) {
19+
return null;
20+
}
21+
int val = preorder[idx];
22+
idx++;
23+
TreeNode root = new TreeNode(val);
24+
root.left = helper(preorder, lower, val);
25+
root.right = helper(preorder, val, upper);
26+
return root;
27+
}
28+
}

Medium/Maximum Binary Tree.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,35 @@
88
* }
99
*/
1010
class Solution {
11-
public TreeNode constructMaximumBinaryTree(int[] nums) {
12-
if (nums.length == 0) {
13-
return null;
14-
}
15-
16-
return constructTree(nums, 0, nums.length-1);
11+
public TreeNode constructMaximumBinaryTree(int[] nums) {
12+
if (nums.length == 0) {
13+
return null;
1714
}
18-
19-
private TreeNode constructTree(int[] nums, int start, int end) {
20-
if (start > end) {
21-
return null;
22-
}
23-
24-
int idx = start;
25-
for (int i=start+1; i<=end; i++) {
26-
if (nums[i] > nums[idx]) {
27-
idx = i;
28-
}
29-
}
30-
31-
TreeNode root = new TreeNode(nums[idx]);
32-
33-
root.left = constructTree(nums, start, idx-1);
34-
root.right = constructTree(nums, idx+1, end);
35-
36-
return root;
15+
TreeNode root = helper(nums, 0, nums.length - 1);
16+
return root;
17+
}
18+
19+
private TreeNode helper(int[] nums, int start, int end) {
20+
if (start > end) {
21+
return null;
3722
}
23+
int maxIdx = getMaxIdx(nums, start, end);
24+
TreeNode root = new TreeNode(nums[maxIdx]);
25+
root.left = helper(nums, start, maxIdx - 1);
26+
root.right = helper(nums, maxIdx + 1, end);
27+
return root;
28+
}
29+
30+
private int getMaxIdx(int[] nums, int start, int end) {
31+
int maxVal = Integer.MIN_VALUE;
32+
int maxIdx = -1;
33+
while (start <= end) {
34+
if (nums[start] > maxVal) {
35+
maxVal = nums[start];
36+
maxIdx = start;
37+
}
38+
start++;
39+
}
40+
return maxIdx;
41+
}
3842
}

0 commit comments

Comments
 (0)