
You are given two strings, 'a' and 'b'.
Your task is to find the minimum number of times you need to repeat string 'a' such that string 'b' becomes a substring of the resulting string. If it's impossible for 'b' to be a substring of a repeated 'a', you should return -1.
For example, repeating "abc" 3 times results in the string "abcabcabc".
The first line of input contains the string 'a'.
The second line of input contains the string 'b'.
Print a single integer representing the minimum number of repetitions of 'a' required, or -1 if it's impossible.
Repeating a string 0 times results in an empty string.
The strings 'a' and 'b' consist of lowercase English letters.
abcd
cdabcdab
3
We repeat string 'a' ("abcd") three times to get "abcdabcdabcd".
The string 'b' ("cdabcdab") is a substring of this resulting string. Two repetitions ("abcdabcd") are not enough.
a
aa
2
Repeating 'a' ("a") two times gives "aa". The string 'b' ("aa") is a substring (in this case, equal to) the result.
The expected time complexity is O(N * M), where N is the length of string 'a' and M is the length of string 'b'. Using efficient substring search algorithms, this can be improved to O(N+M).
1 <= a.length, b.length <= 10^4
'a' and 'b' consist of lowercase English letters.
Time limit: 1 sec
The expected time complexity is O(N * M), where N is the length of string 'a' and M is the length of string 'b'. Using efficient substring search algorithms, this can be improved to O(N+M).