Repeated Substring Pattern

Easy

Topics
StringString Matching

Given a string s, return true if it can be built by repeating a substring of it multiple times.

Example 1

abab
the repeating unit
The shaded block repeats to fill the string, so the answer is true. Only lengths that divide 4 are worth testing. The slick check is to search for the string inside its own doubled copy with the first and last characters removed.
Cells are the characters of the string; the small numbers underneath are indices.
Input:  s = "abab"
Output: true

Example 2

aba
No block shorter than the whole string repeats to fill it, so the answer is false. Only divisors of 3 can work, which keeps the search small.
Input:  s = "aba"
Output: false

Example 3

abcabcabc
the repeating unit
Input:  s = "abcabcabc"
Output: true

Constraints

  • 1 <= s.length <= 10^4
  • s consists of lowercase English letters.
Editorial — intuition, approach, complexity and pitfalls

Intuition

The direct approach tries every candidate period: for each divisor d of n, check whether repeating the first d characters n / d times reproduces the string. That is O(n * number of divisors) and is a perfectly good answer.

The one-liner is a genuinely clever identity. Concatenate the string with itself and strip one character from each end; the original appears inside that if and only if it is built from a repeated block.

s = "abab"
s + s = "abababab"
trim one from each end = "bababa"
does "abab" appear inside?  yes  -> true

s = "aba"
s + s = "abaaba"
trimmed = "baab"
does "aba" appear?  no  -> false

Why it works: in s + s the original obviously occurs at offset 0 and at offset n. Trimming one character from each end removes exactly those two occurrences. Any other occurrence sits at some offset k with 0 < k < n, and s appearing at offset k in its own doubling means s is invariant under rotation by k — which forces s to be a repetition of a block of length gcd(k, n). So an inner occurrence exists precisely when a repeated block does.

Approach

  1. Return whether s is a substring of (s + s)[1:-1].

Dry Run

s = "abab"
  s + s                   = "abababab"
  trim one from each end  = "bababa"
  find("abab")            = 1    -> true
  and the period really is 2:  "ab" * 2 == "abab"
s = "aba"
  s + s                   = "abaaba"
  trimmed                 = "baab"
  find("aba")             = -1   -> false
  no divisor works either: the only candidate is 1, and "a" * 3 = "aaa"
s = "abcabcabc"
  s + s                   = "abcabcabcabcabcabc"
  trimmed                 = "bcabcabcabcabcab"
  find("abcabcabc")       = 2    -> true
  the period is 3, not 4 -- testing only n / 2 would call this one false
The offset that find returns is not incidental, it is the period. Add back the one
character stripped from the front: "abab" is found at trimmed offset 1, so offset 2
in s + s, and 2 is the block length. "abcabcabc" is found at trimmed offset 2, so
offset 3, and 3 is the block length.
That makes the rotation argument concrete. An occurrence at offset k inside the
doubling says s is unchanged by rotating it k places, and a string invariant under
a non-trivial rotation has to be a repeated block. The two trivial occurrences, at
offset 0 and offset n, are exactly what the one-character trim on each end removes,
which is why both ends must be trimmed and why trimming two from one end does not
work.

Complexity

  • Time — O(n) if the substring search is linear (KMP-based, as in most standard libraries); a naive search makes it O(n^2). Worth naming, since the one-liner's cost is hidden inside the library call.
  • Space — O(n): the doubled string. The divisor-checking approach is O(1) space, which is the reason to prefer it if space is constrained.

Common Pitfalls

  • Forgetting to trim both ends, which leaves the trivial occurrences and returns true for everything.
  • Trimming two characters from one end instead of one from each. That removes the offset-0 occurrence twice and still admits the one at offset n - 1.
  • Claiming O(1) space for the one-liner; it builds a string of length 2n.
  • Presenting the trick without the rotation argument. The follow-up is always "why does that work?".
  • Checking only the single candidate period n / 2. A string like "abcabcabc" has period 3, not 4.

Full Solution — all languages

class Solution:
    def repeatedSubstringPattern(self, s):
        return s in (s + s)[1:-1]
Asked at Myntra
SensAI
Beta
Tap the mic to stop voice mode