459. 重复的子字符串
class Solution {
public boolean repeatedSubstringPattern(String s) {
if (s == null || s.length() < 2) {
return false;
}
int length = s.length();
StringBuilder sb;
String temp;
for (int i = 1; i <= length / 2; i++) { //暴力..
if (length % i != 0) {
continue;
}
int n = length / i;
sb = new StringBuilder();
temp = s.substring(0, i);
for (int j = 0; j < n; j++) {
sb.append(temp);
}
if (sb.toString().equals(s)) {
return true;
}
}
return false;
}
}
神仙方法:
class Solution {
public boolean repeatedSubstringPattern(String s) {
String newStr = s + s;
newStr = newStr.substring(1, newStr.length() - 1); // 掐头去尾
return newStr.contains(s);
}
}