Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Example 1:
Input: "abab" Output: True Explanation: It's the substring "ab" twice.
Example 2:
Input: "aba" Output: False
Example 3:
Input: "abcabcabcabc" Output: True Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
枚举子串长度,然后检测子串是否符合条件。
public class Solution {
public boolean repeatedSubstringPattern(String str) {
int k;
int len = str.length();
String s1;
String s2;
int i;
for(k = 1; k < len; k ++){
if(len % k != 0) continue;
s1 = str.substring(0, k);
for(i = 0; i < len; i += k){
s2 = str.substring(i, i + k);
if(s1.equals(s2) == false) break;
}
if(i == len) return true;
}
return false;
}
}