Skip to content

Eason #4

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 6 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
16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_12.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions leetcode-algorithms.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
10 changes: 8 additions & 2 deletions src/main/java/com/fishercoder/solutions/_451.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
*/
public class _451 {

public String frequencySort(String s) {
public static String frequencySort(String s) {
Map<Character, Integer> 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<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue()));
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -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));
}
}
26 changes: 23 additions & 3 deletions src/main/java/com/fishercoder/solutions/_459.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,36 @@ 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;
else cur = pattern[cur-1];//start from beginning of current matching pattern.
}
}

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
Expand All @@ -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));
}
}