My clean and simple C++ Solution without KMP


  • 0
    S
    class Solution {
    public:
        bool repeatedSubstringPattern(string s) {
            int len = s.length();
            for(int i = 0; i < len-1; i++){
                if(len % (i+1)) continue;
                int n = len / (i+1);
                string sub = s.substr(0, i+1);
                string newStr = "";
                while(n--) newStr += sub;
                if(newStr == s) return true;
            }
            return false;
        }
    };

Log in to reply
 

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