• 0

    Basic idea is we check every possible repeating length and find out whether it is a repeated string.

    class Solution {
    bool repeatedSubstringPattern(string str) {
        if(str.size() <= 1) return false;
        for(int len = 1; len <= str.size()/2; ++len){
            if(check(str,len)) return true;
        return false;
    bool check(string str, int len){
        if(str.size() % len != 0) return false;// because the len should be a multiple of string length.
        for(int i = 0; i < str.size(); ++i){
            if(str[i] != str[(i+len)%str.size()]) return false;
        return true;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.