-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
44 lines (34 loc) · 1.21 KB
/
Copy pathSolution.java
File metadata and controls
44 lines (34 loc) · 1.21 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
public class Solution {
public boolean repeatedSubstringPattern(String str) {
if (str == null)
return false;
if (str.length() <= 1)
return false;
int strLen = str.length();
int halfStrLen = strLen >> 1;
int sameCharIdx = 1;
while (sameCharIdx <= halfStrLen) {
if (str.charAt(sameCharIdx) == str.charAt(0)) {
if (repeatedSubstringPattern(str, sameCharIdx))
return true;
}
sameCharIdx++;
}
return false;
}
private boolean repeatedSubstringPattern(String str, int sameCharIdx) {
String subStr = str.substring(0, sameCharIdx);
int subStrLen = subStr.length();
int strLen = str.length();
if (strLen % subStrLen == 0) {
int repeatedTimes = strLen / subStrLen;
StringBuilder sb = new StringBuilder(strLen);
for (int i = 0; i < repeatedTimes; i++) {
sb.append(subStr);
}
if (str.equals(sb.toString()))
return true;
}
return false;
}
}