🚀 Phases 1–5 are live — Days 1–17 cover the foundations and the algorithmic patterns. See the roadmap →

Repeated Substring Pattern easy

Description

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

Examples

> Case 1:
    s = "abab"
    Output: true
    Explanation: It is the substring "ab" twice.
 
> Case 2:
    s = "aba"
    Output: false
 
> Case 3:
    s = "abcabcabcabc"
    Output: true
    Explanation: It is the substring "abc" four times.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of lowercase English letters.

State design / Intuition

Approach 1: Rotation trick (O(n))

If s is formed by repeating substring t, then s + s (with the first and last character removed) still contains s as a substring. This is because the “seam” between the two copies always contains the original s. Checking if s appears in (s + s)[1:-1] is a clean O(n) solution.

return s in (s + s)[1:-1]

Approach 2: KMP failure function (O(n))

The KMP failure function directly encodes the period of the string. For fail = build_fail(s):

  • fail[-1] = the length of the longest proper prefix of s that is also a suffix.
  • Let k = n - fail[-1]. If n % k == 0, then k is the minimal period of s — and s is a repetition of its length-k prefix.

This is the cleanest O(n) approach with no string concatenation.

Code

The condition fail[-1] > 0 ensures the string has a non-trivial border (a proper prefix equal to a suffix). Without it, period = n and the only “repeating unit” is the whole string — not a valid repetition. The condition n % period == 0 ensures the period divides evenly — for "abab", period = 2, 4 % 2 == 0 → valid.

Analysis

  • Time: O(n) — one pass to build the failure function.
  • Space: O(n) — the failure function array.

The rotation trick s in (s+s)[1:-1] is equally O(n) if using KMP for the in check. Python’s in for strings is O(nm) in the worst case (same as naive search), so for strictness, the KMP failure function approach is cleaner.

Same skin

  • Smallest period of a stringn - fail[-1] gives the length of the minimal period.
  • Longest repeated prefixfail[-1] gives the length of the longest prefix that also appears as a suffix.
  • Check if string can be halved — the rotation trick s in (s+s)[1:-1] generalizes to checking string rotation equivalence.
  • Count of distinct rotations — related to the number of distinct periods.