class Solution {
public:
vector<int> getNext(string s) {
vector<int> next(s.size(), 0);
int j = 0;
for(int i = 1; i < s.size(); i++) {
while(j > 0 && s[i] != s[j]) {
j = next[j - 1];
}
if(s[i] == s[j]) {
j++;
}
next[i] = j;
}
return next;
}
bool repeatedSubstringPattern(string s) {
string doubleS = s + s;
doubleS.erase(0, 1);
doubleS.erase(doubleS.size() - 1);
int m = s.size();
int n = doubleS.size();
vector<int> next = getNext(s);
int i = 0, j = 0;
while(i < n) {
while(j > 0 && doubleS[i] != s[j]) {
j = next[j - 1];
}
if(doubleS[i] == s[j]) {
j++;
}
i++;
if(j == m) {
return true;
}
}
return false;
}
};
08-10
514

02-02
1088

04-21
922
