s4553711
10/23/2017 - 2:20 PM

459.cpp

class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        string str = s;
        int len = s.size();
        if (len < 1) return false;
        for(int i = 1; i <= len / 2; i++) {
            //cout << i << endl;
            if (len % i == 0) {
                //cout << "b: " << str << endl;
                str = shift(s, i);
                //cout << "a: " << str << endl;
                if (str == s) return true;
            }
        }
        return false;
    }
    
    string shift(string &str, int i) {
        string ret = str.substr(i);
        ret += str.substr(0, i);
        return ret;
    }
};