Repeated String Match

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 impossible for b to be a substring of a after repeating it, return -1.

Notice: string "abc" repeated 0 times is "", repeated 1 time is "abc", and repeated 2 times is "abcabc".

Example 1
Inputa = "abcd", b = "cdabcdab"
Output3
We return 3 because by repeating a three times, "abcdabcdabcd" contains b as a substring.
Example 2
Inputa = "a", b = "aa"
Output2
Repeating a twice gives "aa", which contains b as a substring.

Constraints

  • 1 <= a.length, b.length <= 10^4
  • a and b consist of lowercase English letters.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate