TRT
Sign in
easyAdvancedLeetCode#prefix function#periodicity

Repeated substring pattern

Decide whether a string is a shorter block repeated several times, using its smallest period from the prefix function.

The Runtime Theory Team1 min read
Taught inKMP and prefix functionsPro — members onlyThe prefix function records, for every position of a pattern, the longest proper prefix that is also a suffix there. Knuth-Morris-Pratt uses it to search a text without ever moving backward, in O(n + m) time in the worst case.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

ins = "abab"

outtrue

ins = "aba"

outfalse

ins = "abcabcabcabc"

outtrue

ins = "a"

outfalse

Return true if s can be constructed by taking a substring of it and appending multiple copies of that substring together.

Before you code

If s is a block repeated, how does s overlap with itself, and what is its longest border?

Hints

  1. Compute the prefix function of s. Let L = π[n − 1], the length of the longest border.
  2. n − L is the smallest period of s.
  3. The answer is true exactly when L > 0 and n is divisible by n − L. Check it on aba, where the period 2 doesn't divide 3.

Production & complexity notes

When solving Repeated substring pattern on LeetCode (easy), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in KMP and prefix functions to reinforce the underlying pattern.

What it practices

Reading periods from the prefix function, from KMP and prefix functions.

Not started

Sign in to save your learning progress.

Sign in to save