diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000..905fcd37d5 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000000..d411041745 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000000..f58bbc1127 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..d30d09e204 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..032a358cd2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..94a25f7f4c --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/leetcode-algorithms.iml b/leetcode-algorithms.iml new file mode 100644 index 0000000000..4e3316bc3d --- /dev/null +++ b/leetcode-algorithms.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/fishercoder/solutions/_451.java b/src/main/java/com/fishercoder/solutions/_451.java index 9e46628547..307a570619 100644 --- a/src/main/java/com/fishercoder/solutions/_451.java +++ b/src/main/java/com/fishercoder/solutions/_451.java @@ -44,9 +44,11 @@ */ public class _451 { - public String frequencySort(String s) { + public static String frequencySort(String s) { Map map = new HashMap(); - for (char c : s.toCharArray()) map.put(c, map.getOrDefault(c, 0) + 1); + for (char c : s.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } List> list = new ArrayList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue())); StringBuilder stringBuilder = new StringBuilder(); @@ -57,4 +59,8 @@ public String frequencySort(String s) { } return stringBuilder.toString(); } + public static void main(String[] args) { + String s = "trete" ; + System.out.println(frequencySort(s)); + } } diff --git a/src/main/java/com/fishercoder/solutions/_459.java b/src/main/java/com/fishercoder/solutions/_459.java index fa79948304..6abc0ff4c7 100644 --- a/src/main/java/com/fishercoder/solutions/_459.java +++ b/src/main/java/com/fishercoder/solutions/_459.java @@ -27,7 +27,7 @@ public static boolean repeatedSubstringPattern(String str) { pattern[0] = 0; while (j < n) { - if (str.charAt(cur) == str.charAt(j)){ + if (str.charAt(cur) == str.charAt(j)) { pattern[j++] = ++cur; } else { if (cur == 0) pattern[j++] = 0; @@ -35,9 +35,28 @@ public static boolean repeatedSubstringPattern(String str) { } } - return (pattern[n-1] > 0 && n%(n-pattern[n-1]) == 0); + 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: + 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 +66,6 @@ public static void main(String...args){ // String str = "abababc"; String str = "abababaaba"; System.out.println(repeatedSubstringPattern(str)); + System.out.println(repeatedSubstringPattern_2(str)); } }