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) {if(str.length()<2) return false;
char[] arr=str.toCharArray();
boolean match=false;
int fast=1,slow=0;
while(fast<arr.length){
if(arr[fast]==arr[0]){
for(slow=0;fast<arr.length;){
if(arr[fast]==arr[slow]){
match=true;
fast++;
slow++;
}
else{
match=false;
break;
}
}
}
else{
match=false;
fast++;
}
}
return match&&slow*2>=arr.length;
}
}
本文介绍了一种使用快慢指针的方法来判断一个非空字符串是否可以通过其子串的多次重复拼接而成。通过实例展示了如何利用该算法进行验证。

906

被折叠的 条评论
为什么被折叠?



