C++ short O(N^2) solution using modulus.


  • 0
    T
    class Solution {
    public:
        bool repeatedSubstringPattern(string s) {
            for (int i = 1; i <= s.size()/2; i++) {
                if (s.size() % i == 0) {
                     for (int j = i; j < s.size(); j++) {
                         // i here is the period. 
                         if (s[j % i] != s[j]) break; 
                         if (j==s.size()-1) return true;
                     }
                }
            }
            return false;
        }
    };
    

Log in to reply
 

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