-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path459.java
More file actions
64 lines (63 loc) · 1.9 KB
/
459.java
File metadata and controls
64 lines (63 loc) · 1.9 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public boolean repeatedSubstringPattern(String s) {
int len = s.length();
if (len <= 1) {
return false;
}
char last = s.charAt(len - 1);
int val = s.lastIndexOf(last, len / 2 - 1) + 1;
while (val > 0) {
if (len % val == 0) {
String pat = s.substring(0, val);
boolean res = true;
for (int i = val; i < len; i += val) {
if (!s.substring(i, i + val).equals(pat)) {
res = false;
break;
}
}
if (res) {
return res;
}
}
val = s.lastIndexOf(last, val - 2) + 1;
}
return false;
}
}
__________________________________________________________________________________________________
sample 38048 kb submission
class Solution {
public boolean repeatedSubstringPattern(String s) {
int[] prefix = kmp(s);
int len = prefix[s.length()-1];
int n = s.length();
return (len > 0 && n%(n-len) == 0);
}
public int[] kmp(String s){
int n = s.length();
char[] ch = s.toCharArray();
int[] res = new int[n];
int i=0;
int j=1;
res[0] = 0;
while(i < ch.length && j < ch.length){
if(ch[j] == ch[i]){
res[j] = i+1;
i++;
j++;
}else{
if(i == 0){
res[j] = 0;
j++;
}else{
i = res[i-1];
}
}
}
return res;
}
}
__________________________________________________________________________________________________