c++ solution without kmp, 32ms


  • 1
    K

    easier to understand

    class Solution {
    public:
        bool repeatedSubstringPattern(string str) {
            int n = str.size(), d = n / 2;
            while (d > 0) {
                if (n % d == 0) {
                    int i = 0;
                    while (i + d < n) {
                        if (str.substr(i, d) != str.substr(i+d, d)) break;
                        i += d;
                    }
                    if (i + d == n && str.substr(i - d, d) == str.substr(i, d)) return true;
                }
                --d;
            }
            return false;
        }
    };
    

Log in to reply
 

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