459. 重复的子字符串 - 力扣(LeetCode) (leetcode-cn.com)https://leetcode-cn.com/problems/repeated-substring-pattern/KMP不太熟练,就先暴力做一下。
class Solution {
public boolean repeatedSubstringPattern(String s) {
int subLength = 1;//子串长度
while (subLength < s.length()) {
if (s.length() % subLength != 0) {//不是整倍数就跳过
subLength++;
continue;
}
boolean flag=true;
for (int i = subLength; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(i % subLength)) {//不一样就跳过
flag = false;
break;
}
}
if(flag)//没有不一样的,返回true
return true;
subLength++;
}
return false;
}
}