class Solution {
public:
bool repeatedSubstringPattern(string str) {
if(str.length() == 1)
return false;
cout<<str.length()<<endl;
int *pre = new int[str.length()+1];
pre[0] = -1;
int i = 0, j = 0;
for(i = 1; i < str.length(); ){
if(j == -1 || str[i] == str[j]){
pre[++i] = ++j;
}
else
j = pre[j];
}
int m = str.length()-pre[str.length()];
if( m == str.length() )return 0;
if(str.length() % m == 0)
return true;
return false;
}
};
leetcode 459. Repeated Substring Pattern
字符串重复子串检测算法
最新推荐文章于 2025-12-04 23:36:16 发布
本文介绍了一种用于检测字符串中是否存在重复子串的高效算法。该算法通过构建前缀数组来快速判断输入字符串是否由一个更短的子串重复组成,并提供了完整的C++实现代码。
4113

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



