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
Visualization
Time & Space Complexity
We only check at most 3 possible repetition counts, each taking O(n + m) time for substring search
We store at most (len(b)//len(a) + 2) repetitions of string a
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