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
- Compute the prefix function of s. Let L = π[n − 1], the length of the longest border.
- n − L is the smallest period of s.
- 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.