We’re preparing your current view and syncing the latest data.
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of the repeated string. If no such solution exists, return -1. For example, with A = 'abcd' and B = 'cdabcdab', by repeating A 3 times 'abcdabcdabcd', B is a substring of it. So the answer is 3.
Two strings A and B.
An integer representing the minimum number of times A must be repeated so that B is a substring of the repeated string. Return -1 if impossible.
1 <= A.length, B.length <= 10^4
Example 1
Input
A = 'abcd', B = 'cdabcdab'
Output
3
Explanation
Repeating 'abcd' three times gives 'abcdabcdabcd', which contains 'cdabcdab' as a substring.
Example 2
Input
A = 'a', B = 'aa'
Output
2
Explanation
'a' repeated twice is 'aa', which contains B as a substring.
Example 3
Input
A = 'abc', B = 'wxyz'
Output
-1
Explanation
It's impossible for any number of repeated 'abc' to contain 'wxyz'.