Skip to content

Eason #5

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 8 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
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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you missing a link or something here?

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));
}
}