-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepeatedSubstrFinder.java
72 lines (57 loc) · 1.93 KB
/
RepeatedSubstrFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package org.sean.string;
import java.util.Arrays;
/** * 459. Repeated Substring Pattern */
public class RepeatedSubstrFinder {
// Solution 1: If the substring is repeated, it must be in the range [0, length/2]
public boolean repeatedSubstringPattern0(String s) {
if (s == null || s.length() <= 1) {
return false;
}
int length = s.length();
int range = length / 2;
System.out.println(Arrays.toString(prefixFunction(s)));
for (int i = 1; i <= range; i++) {
String preStr = s.substring(0, i);
String post = s.substring(i);
String dup = post + preStr;
if (dup.equals(s)) {
return true;
}
}
return false;
}
// Solution 2: Use the pre-calculated prefix function.
//
// https://cp-algorithms.com/string/prefix-function.html
public boolean repeatedSubstringPattern1(String s) {
if (s == null || s.length() <= 1) {
return false;
}
int[] prefixes = prefixFunction(s);
System.out.println(Arrays.toString(prefixes));
int length = s.length();
int lastPrefixCnt = prefixes[length - 1];
int k = length - lastPrefixCnt;
return lastPrefixCnt > 0 && length % k == 0;
}
// prefix function
private int[] prefixFunction(String s) {
int n = s.length();
int[] pi = new int[n];
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) j = pi[j - 1];
if (s.charAt(i) == s.charAt(j)) j++;
pi[i] = j;
}
return pi;
}
// Solution 3: Concate the rotated string
public boolean repeatedSubstringPattern(String s) {
if (s == null || s.length() <= 1) {
return false;
}
String ss = s + s;
return ss.substring(1, ss.length() - 1).contains(s);
}
}