Formatted question description: https://leetcode.ca/all/459.html

459. Repeated Substring Pattern (Easy)

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

 

Example 1:

Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.

Example 2:

Input: "aba"
Output: False

Example 3:

Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

Related Topics:
String

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/repeated-substring-pattern/

// Time: O(N^2)
// Space: O(1)
class Solution {
    bool match(string &s, int start, int len) {
        for (int i = 0; i < len; ++i) {
            if (s[i] != s[start + i]) return false;
        }
        return true;
    }
public:
    bool repeatedSubstringPattern(string s) {
        for (int len = s.size() / 2; len >= 1; --len) {
            if (s.size() % len) continue;
            int i = len;
            for (; i <= s.size() - len && match(s, i, len); i += len);
            if (i >= s.size()) return true;
        }
        return false;
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/repeated-substring-pattern/

// Time: O(N^2)
// Space: O(1)
class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        string_view p = s, sv = s;
        for (int len = s.size() / 2; len >= 1; --len) {
            if (s.size() % len) continue;
            p = p.substr(0, len);
            int i = s.size() / len - 1;
            for (; i >= 0 && p == sv.substr(i * len, len); --i);
            if (i < 0) return true;
        }
        return false;
    }
};

Solution 3.

// OJ: https://leetcode.com/problems/repeated-substring-pattern/

// Time: O(N)
// Space: O(N)
// Ref: https://leetcode.com/problems/repeated-substring-pattern/discuss/94397/C%2B%2B-O(n)-using-KMP-32ms-8-lines-of-code-with-brief-explanation.
class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        int k = -1, N = s.size();
        vector<int> pi(N + 1, -1);
        for (int i = 1; i <= N; ++i) {
            while (k >= 0 && s[k] != s[i - 1]) k = pi[k];
            pi[i] = ++k;
        }
        return pi[N] && (pi[N] % (N - pi[N]) == 0);
    }
};

Java

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        if (s == null || s.length() <= 1)
            return false;
        int length = s.length();
        StringBuffer subStringBuffer = new StringBuffer();
        int maxSublength = length / 2;
        for (int i = 0; i < maxSublength; i++) {
            subStringBuffer.append(s.charAt(i));
            int curLength = i + 1;
            if (length % curLength != 0)
                continue;
            int times = length / curLength;
            StringBuffer temp = new StringBuffer();
            for (int j = 0; j < times; j++)
                temp.append(subStringBuffer);
            if (temp.toString().equals(s))
                return true;
        }
        return false;
    }
}

All Problems

All Solutions