Repeated String Match - Problem

Imagine you have a repeating pattern and need to find if another string can be found within that pattern when repeated enough times. Given two strings a and b, your task is to find the minimum number of times you need to repeat string a so that string b becomes a substring of the repeated pattern.

For example, if a = "abc" and b = "cabca", repeating a twice gives us "abcabc", and we can find b as a substring: "abcabcabc" (starting from position 2). So the answer would be 3 repetitions.

If it's impossible for b to be a substring of any number of repetitions of a, return -1.

Note: String "abc" repeated 0 times is "", repeated 1 time is "abc", and repeated 2 times is "abcabc".

Input & Output

example_1.py β€” Basic Case
$ Input: a = "abcd", b = "cdabcdab"
β€Ί Output: 3
πŸ’‘ Note: We need to repeat "abcd" three times to get "abcdabcdabcd". The string "cdabcdab" appears as a substring starting at index 2.
example_2.py β€” Single Character
$ Input: a = "a", b = "aa"
β€Ί Output: 2
πŸ’‘ Note: We need to repeat "a" twice to get "aa", which exactly matches string b.
example_3.py β€” Impossible Case
$ Input: a = "abc", b = "wxyz"
β€Ί Output: -1
πŸ’‘ Note: String b contains characters 'w', 'x', 'y', 'z' that don't exist in string a, so it's impossible for b to be a substring of any repetition of a.

Visualization

Tap to expand
🎡 Musical Pattern Matching🎼 Pattern (a): "abc" β†’ Notes: A-B-C🎡 Target Melody (b): "cabca" β†’ Sequence: C-A-B-C-Aβœ“ All melody notes exist in our pattern!πŸ“ Minimum repetitions needed: ⌈5/3βŒ‰ = 2πŸ”„ Try 2 repetitions: "abcabc"Pattern: a-b-c-a-b-cMelody: c-a-b-c-a β†’ Not found as continuous sequenceπŸ”„ Try 3 repetitions: "abcabcabc"Pattern: a-b-c-a-b-c-a-b-cMelody: c-a-b-c-a (found starting at position 2!)🎯 Success! Answer: 3 repetitionsπŸ’‘ Key Insight:We never need more than ⌈len(melody)/len(pattern)βŒ‰ + 1 repetitions!
Understanding the Visualization
1
Pattern Analysis
Analyze what notes (characters) are available in our repeating pattern
2
Melody Validation
Check if our target melody uses only available notes
3
Calculate Repetitions
Mathematically determine minimum pattern repetitions needed
4
Pattern Matching
Test the calculated repetitions to find our melody
Key Takeaway
🎯 Key Insight: Like finding a melody in music, we can mathematically calculate the minimum repetitions needed and validate efficiently using character set theory.

Time & Space Complexity

Time Complexity
⏱️
O(n + m)

We only check at most 3 possible repetition counts, each taking O(n + m) time for substring search

n
2n
βœ“ Linear Growth
Space Complexity
O(n + m)

We store at most (len(b)//len(a) + 2) repetitions of string a

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ a.length, b.length ≀ 104
  • a and b consist of lowercase English letters
  • Key insight: You never need more than ⌈len(b)/len(a)βŒ‰ + 1 repetitions
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
67.2K Views
Medium Frequency
~18 min Avg. Time
1.8K 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