-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPeriod.java
More file actions
75 lines (66 loc) · 2.46 KB
/
Copy pathStringPeriod.java
File metadata and controls
75 lines (66 loc) · 2.46 KB
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
73
74
75
package algorithm;
/*
* 1. 返回一个str是否是周期串
* 2. 找出str的最小周期
* */
public class StringPeriod {
/* 1.find len of the longest proper prefix of ‘str’,
* Let the length of the longest proper prefix suffix be ‘len’.
* time :o(n) -- KMP
*/
private static int[] prefix; //如何设置更好????
private static void computeLPSArray (String s) {
// len of previous longest prefix suffix??
int len = 0;
prefix[0] = 0; // prefix[0] is always 0
int i = 1;
//for i = 1 to s.len - 1,calculate lps[]
while (i < s.length()) {
if (s.charAt(i) == s.charAt(len)) {
len++;
prefix[i] = len;
i++;
} else {
if (len != 0) { //This is tricky, exp: AAACAAAA and i = 7.
len = prefix[len - 1]; // also we do not increment i here
} else {
prefix[0] = 0;
i++;
}
}
}
}
// Returns true if str is repetition of one of its substrings else return false.
public static boolean isRepeat(String s) {
// Find length of string and create an array to store lps values used in KMP
int n = s.length();
prefix = new int[n];
computeLPSArray(s);
// Find length of longest suffix which is also prefix of str.
int maxLen = prefix[n-1];
// If there exist a suffix which is also prefix AND Length of the remaining substring
// divides total length, then str[0..n-len-1] is the substring that repeats n/(n-len)
// times (Readers can print substring and value of n/(n-len) for more clarity.
return (maxLen > 0 && n %(n - maxLen) == 0)? true : false;
}
public static int getRepeat(String s) {
if (isRepeat(s)) {
int maxLen = prefix[s.length() - 1];
return Math.min(s.length() - maxLen, maxLen);
}
return 0;
}
public static void main(String[] args) {
String txt[] = {"ABCABC", "ABABAB", "ABCDABCD",
"GEEKSFORGEEKS", "GEEKGEEK",
"AAAACAAAAC", "ABCDABC"};
int n = txt.length;
for (int i = 0; i < n; i++) {
if(isRepeat(txt[i]) == true)
System.out.print("True");
else
System.out.print("False");
System.out.println(" " + getRepeat(txt[i]));
}
}
}