String Matching Pattern matching
582. Repeated String Match
You are given two strings, a and b. Write a out several times in a row, with nothing between the copies.
Return the smallest number of copies that makes b appear in the result, or -1 if no number of copies does.
b appears in a string when its letters show up there in order and next to each other. For example, three
copies of "ab" give "ababab", and "bab" appears in it.
Example 1
Input: a = "xyz", b = "zxyzx"
3Explanation: Two copies give xyzxyz. That is long enough, but zxyzx does not appear in it. Three copies give
xyzxyzxyz, and zxyzx starts at index 2.
Example 2
Input: a = "ab", b = "bb"
-1Explanation: Every b in the copies is followed by an a, so two b letters never sit next to each other.
Example 3
Input: a = "hello", b = "ohe"
2Explanation: b is shorter than a, but it runs across the join of two copies: hellohello has ohe at index 4.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ a.length, b.length ≤ 105aandbhold only lowercase English letters.
Clarifying questions
What you would ask an interviewer before writing any code.