Description

Given strings a and b, find minimum number of times a needs to be repeated such that b is a substring, or -1 if impossible.

Examples

Input:a = "abcd", b = "cdabcdab"
Output:3
Explanation:

After 3 repeats: 'abcdabcdabcd' contains 'cdabcdab'.

Input:a = "abc", b = "cabcab"
Output:3
Explanation:

After 3 repeats: 'abcabcabc' contains 'cabcab' starting at index 2.

Input:a = "xyz", b = "pqr"
Output:-1
Explanation:

No matter how many times 'xyz' is repeated, it will never contain 'pqr' since they share no common characters.

Constraints

  • 1 ≤ a.length, b.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!