Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is not possible for b to be a substring of the repeated a, return -1.
Repeating "abc" 0 times gives "", 1 time gives "abc", and 2 times gives "abcabc".
Function Signature
def repeated_string_match(a: str, b: str) -> int:
Rules
-
A substring is a contiguous run of characters.
-
Return the smallest count
k
such that
b
occurs in
a
repeated
k
times. Return
-1
when no count works.
Constraints
-
1 <= len(a), len(b) <= 10^4
-
a
and
b
consist of lowercase English letters.
Examples
Input: a = "abcd", b = "cdabcdab"
Output: 3
Two copies give "abcdabcd", which is as long as b but is not equal to it. Three copies give "abcdabcdabcd", and b starts at index 2.
Input: a = "a", b = "aa"
Output: 2
One copy is shorter than b. Two copies give "aa", which is b itself.
Input: a = "abc", b = "acb"
Output: -1
In any repetition of "abc", the letter after a is always b, so "ac" never appears and b cannot either.