您的位置:首页 > 其它

Leetcode Repeated Substring Pattern

2016-12-31 04:32 453 查看
题意:给出一个字符串,判断其是否由一个子串重复多次构成。

思路:枚举字串长度。

class Solution {
public:
bool repeatedSubstringPattern(string str) {
string s1;
string s2;
for(int i = 1; i < str.length(); i ++) {
if(str.length() % i) continue;

s1 = str.substr(0, i);
bool isR = true;
for(int j = i; j < str.length(); j += i) {
s2 = str.substr(j, i);
if(s1 != s2){
isR = false;
break;
}
}

if(isR) return true;
}

return false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode