Skip to content

Add solution #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/com/fishercoder/solutions/_459.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,27 @@ public static boolean repeatedSubstringPattern(String str) {

return (pattern[n-1] > 0 && n%(n-pattern[n-1]) == 0);
}
//// the idea is that the length substring will be divisor of the length s, then find the
//sub string and append s.length/sub.length times to check if the string is equaled to the original string.
//credit: https://leetcode.com/problems/repeated-substring-pattern/discuss/

public static boolean repeatedSubstringPattern_2(String s) {
int len = s.length();
for (int i = len/2; i >=1; i--) {
if (len % i == 0) {
int n = len / i;
String sub = s.substring(0, i);
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
sb.append(sub);
}
if (sb.toString().equals(s)) {
return true;
}
}
}
return false;
}
public static void main(String...args){
// String str = "aba";
// String str = "abab";//should be true
Expand All @@ -47,5 +67,6 @@ public static void main(String...args){
// String str = "abababc";
String str = "abababaaba";
System.out.println(repeatedSubstringPattern(str));
System.out.println(repeatedSubstringPattern_2(str));
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/fishercoder/solutions/_508.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,35 @@ private int postOrder(TreeNode root, Map<TreeNode, Integer> map) {
//a more concise and space-efficient solution: https://discuss.leetcode.com/topic/77775/verbose-java-solution-postorder-traverse-hashmap-18ms
//the key difference between the above post and my original solution is that it's using Frequency as the key of the HashMap
}
public class Solution {
Map<Integer, Integer> map;// record the sub sums and their frequence
int max;
public int[] findFrequentTreeSum(TreeNode root) {
map = new HashMap<Integer, Integer>();
max = 0;
helper(root);// post order to put the subsum to the map
List<Integer> list = new ArrayList<>();
for (int key : map.keySet()) {
if (map.get(key) == max) {
list.add(key);
}
}
int[] res = new int[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
return res;
}
public int helper(TreeNode root) {
if (root ==null) {
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
int sum = left + right + root.val;
map.put(sum, map.getOrDefault(sum, 0) + 1);
max = Math.max(max, map.get(sum));//update the max frequence each time we put in a sum to the map.
return sum;

}
}
18 changes: 18 additions & 0 deletions src/main/java/com/fishercoder/solutions/_538.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,23 @@ private void putNodeToList(List<Integer> list, TreeNode root) {
if (root.left != null) putNodeToList(list, root.left);
if (root.right != null) putNodeToList(list, root.right);
}
//easy recursive solution for bst, the idea is to do a reverse inorder traversal.
int sum = 0;
public TreeNode convertBST_rec(TreeNode root) {
if(root == null) {
return root;
}
helper(root);
return root;

}
public void helper(TreeNode root) {
if(root == null) {
return;
}
helper(root.right);
root.val += sum;
sum =root.val;
helper(root.left);
}
}