Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
The Repeated Substring Pattern Detection pattern applies when you need the Repeated Substring Pattern Detection technique within the String Manipulation Patterns pattern. Its time complexity is O(n) with KMP-based matching, O(n²) with naive substring search and space complexity O(n). It is used in 2 problems on Thita, including Greatest Common Divisor of Strings and Repeated Substring Pattern. Common variations are Palindrome Check (Two Pointers / Reverse) and Anagram Check (Frequency Count/Sort).
Learn to detect if a string can be constructed by repeating a substring pattern.
Repeated Substring Pattern Detection is one of the 6 subpatterns of the String Manipulation Patterns pattern, which covers palindrome checking, anagram detection, pattern matching, and string conversion. The whole pattern is about 2 hours of study. This subpattern is an important variation worth recognising on sight.
Master string manipulation including palindrome checking, anagram detection, string matching algorithms (KMP, Rabin-Karp), and conversion problems. Problems in this subpattern are usually searched for as repeated substring, pattern detection, string repetition, leetcode 459.
Read the theory for Repeated Substring Pattern Detection, then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the String Manipulation Patterns tracker shows this one as covered once you have solved its problems.
Repeated Substring Pattern Detection is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the String Manipulation Patterns guide is the shortest path back to the rest of it.
2 problems, easiest first.
Imagine you’re listening to a catchy song chorus on repeat. After a while, you notice the same melody looping over and over. In programming, strings can behave the same way—sometimes, a string is just a smaller pattern repeated multiple times. Detecting these repeated patterns is at the heart of the Repeated Substring Pattern Detection subpattern.
Think about wrapping paper with a repeating design: if you look closely, you’ll see the same flower or stripe printed again and again. In coding, we often need to check if a string (like "abab") is made by repeating a smaller substring ("ab" in this case) several times. This is especially useful in scenarios like:
This pattern is all about spotting these loops or cycles in strings. Before diving into code, just remember: if you can hum the same tune over and over, you’re already halfway to understanding repeated substring patterns!
Let’s break down the key ideas behind detecting repeated substring patterns:
We want to know: Can the entire string be built by repeating a smaller substring multiple times? For example, "abcabcabc" is "abc" repeated 3 times.
Imagine laying out the string side by side with itself:
1Original: a b a b a b
2Repeat: a b a b a bIf you can align the string with itself after shifting by some amount, you’ve found a repeating pattern.
If the string length is n, the substring must have a length that divides n evenly (so it fits perfectly, no leftovers). For example, a 6-letter string could be made from substrings of length 1, 2, or 3.
This concept is used in:
Let’s build a mental model for solving repeated substring pattern problems.
Double, Trim, Search: s is a repeated pattern if s appears inside (s + s) with its first and last character removed. abab is found at index 1 in bababa (true); aba is not found in baab (false).
"abcabcabc", try pieces of length 1, 2, 3, etc.Brute Force:
n/2:
n % length == 0, repeat the substring and see if it matches the original.String Trick (Concatenation):
"abab" + "abab" = "abababab").KMP Algorithm (Advanced):
n - prefix length (the period) divides n evenly and the prefix is non-empty, it’s a repeated pattern."a" → Not repeated (needs at least 2 repeats)."aaaa" → Yes, repeated "a"."abcd" → No pattern."abab":
"ab" → "ab" + "ab" = "abab" → matches!"aba":
"a" → "a" + "a" + "a" = "aaa" → doesn’t match.| Substring Length | Is Divisor of n? | Repeats to Original? |
|---|---|---|
| 1 | Yes | Maybe |
| 2 | Yes | Maybe |
| ... | ... | ... |
Let’s implement the string concatenation trick (efficient and elegant):
1#include <iostream>
2#include <string>
3using namespace std;
4
5// Function to check if a string can be constructed by repeating a substring
6bool repeatedSubstringPattern(const string& s) {
7 // Double the string and remove the first and last character
8 string doubled = s + s;
9 string trimmed = doubled.substr(1, doubled.size() - 2);
10
11 // If the original string appears in the trimmed string, it's repeated
12 return trimmed.find(s) != string::npos;
13}
14
15int main() {
16 string s1 = "abab";
17 string s2 = "aba";
18 string s3 = "abcabcabc";
19 string s4 = "aaaa";
20
21 cout << boolalpha; // Print 'true' or 'false' instead of 1/0
22 cout << "Is 'abab' a repeated substring pattern? " << repeatedSubstringPattern(s1) << endl; // true
23 cout << "Is 'aba' a repeated substring pattern? " << repeatedSubstringPattern(s2) << endl; // false
24 cout << "Is 'abcabcabc' a repeated substring pattern? " << repeatedSubstringPattern(s3) << endl; // true
25 cout << "Is 'aaaa' a repeated substring pattern? " << repeatedSubstringPattern(s4) << endl; // true
26
27 return 0;
28}Why does this work?
"abab" doubled is "abababab", trimmed is "bababa", which contains "abab".Alternative: Brute Force Approach
1bool repeatedSubstringPatternBrute(const string& s) {
2 int n = s.size();
3 // Try every possible substring length
4 for (int len = 1; len <= n / 2; ++len) {
5 if (n % len == 0) { // Only lengths that divide n evenly
6 string pattern = s.substr(0, len);
7 string built = "";
8 for (int i = 0; i < n / len; ++i)
9 built += pattern;
10 if (built == s)
11 return true;
12 }
13 }
14 return false;
15}Let’s walk through the solution to the "Repeated Substring Pattern" problem step by step.
s can be made by repeating a substring multiple times."abab" → "ab" + "ab" → Yes."abab" + "abab" = "abababab""bababa""abab") inside "bababa".true."a" → After doubling and trimming, nothing is left, so return false."aaaa" → After doubling and trimming, "aaaaaa" contains "aaaa".Signals to Recognize:
"abcabcabc" or "aaaa".Aha! Moments:
Debugging Tips:
Common Misconceptions:
"abac").Memory Trick:
Keep practicing! Every time you spot a repeating chorus in a song or a pattern on a shirt, remember: you’re training your brain for string pattern detection!