Straightforward C++ solution


  • 0
    G
        bool repeatedSubstringPattern(string str) {
            if (str.size() <= 1) return false;
            // check for all substrings starting at 0 index
            for (int len = 1; len <= str.size()/2; len++) {
                if (isRepeated(str.substr(0, len), str.substr(len))) return true;
            }
            return false;
        }
        bool isRepeated(string p, string t) {
            if (t.size() % p.size() != 0) return false;
            for (int i = 0; i < t.size()/p.size(); i++) {
                if (p != t.substr(i * p.size(), p.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.