for(int i = 1; i<=s.size()/2; i++) if(s.substr(i) == s.substr(0,s.size()-i)&&s.size()%i == 0) return true; return false;
@aishiqi why does this work? can you explain?
I am glad to.
say a string s, and a sub string t that repeats, s is constructed by n t(s). so s.size() = n*t.size() ===> s.size()%t.size()==0
if you right shift the string by t.size() chars, the overlap part should be the same.
example: say string s = "abcabcabc", and right shift 3, the overlap part(in parentheses) is the same.
@felder because i is the size of repeated substring, it can't be larget than half of the whole string.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.