diff --git a/src/main/java/com/fishercoder/solutions/_459.java b/src/main/java/com/fishercoder/solutions/_459.java index fa79948304..4aae50e864 100644 --- a/src/main/java/com/fishercoder/solutions/_459.java +++ b/src/main/java/com/fishercoder/solutions/_459.java @@ -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 @@ -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)); } } diff --git a/src/main/java/com/fishercoder/solutions/_508.java b/src/main/java/com/fishercoder/solutions/_508.java index cc84408823..5306d6b48e 100644 --- a/src/main/java/com/fishercoder/solutions/_508.java +++ b/src/main/java/com/fishercoder/solutions/_508.java @@ -81,3 +81,35 @@ private int postOrder(TreeNode root, Map 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 map;// record the sub sums and their frequence + int max; + public int[] findFrequentTreeSum(TreeNode root) { + map = new HashMap(); + max = 0; + helper(root);// post order to put the subsum to the map + List 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; + + } +} \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_538.java b/src/main/java/com/fishercoder/solutions/_538.java index 094bbc7e4d..1d8c6c42e6 100644 --- a/src/main/java/com/fishercoder/solutions/_538.java +++ b/src/main/java/com/fishercoder/solutions/_538.java @@ -61,5 +61,23 @@ private void putNodeToList(List 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); + } }