Repeated Substring Pattern - Problem

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

In other words, determine if the string is made up of a repeating pattern where the same substring is repeated 2 or more times consecutively to form the entire string.

Example: The string "abcabcabc" can be constructed by repeating the substring "abc" three times, so it returns true. However, "abcabcab" cannot be formed by repeating any substring, so it returns false.

Input & Output

Example 1 — Simple Repeat
$ Input: s = "abab"
Output: true
💡 Note: The string can be constructed by repeating "ab" twice: "ab" + "ab" = "abab"
Example 2 — Triple Repeat
$ Input: s = "aba"
Output: false
💡 Note: No substring can be repeated to form "aba". Single characters don't count as patterns.
Example 3 — Long Pattern
$ Input: s = "abcabcabcabc"
Output: true
💡 Note: The pattern "abc" is repeated 4 times to form the string

Constraints

  • 1 ≤ s.length ≤ 104
  • s consists of lowercase English letters

Visualization

Tap to expand
Repeated Substring Pattern DetectionDetermine if string is made by repeating a substringInput String"abcabc"Pattern Found"abc"Verification"abc" × 2 = "abcabc" ✓Break down into:abcabcPattern repeats perfectly → Return true
Understanding the Visualization
1
Input Analysis
Check if string can be split into equal repeating parts
2
Pattern Detection
Test different substring lengths or use concatenation trick
3
Validation
Verify if pattern repeats to form original string
Key Takeaway
🎯 Key Insight: A string has repeating pattern if and only if it appears within (s+s)[1:-1]
Asked in
Google 15 Amazon 12 Facebook 8
125.0K Views
Medium Frequency
~15 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen