Repeated String Match

MediumStringssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two strings a and b. Concatenating a with itself k times gives the string a * k (for example, "abc" repeated 2 times is "abcabc"). Return the **smallest** k >= 1 such that b appears as a contiguous substring of a repeated k times. If no amount of repetition makes b a substring, return -1.

Examples

Input: a = "abcd", b = "cdabcdab"
Output: 3
`"abcdabcdabcd"` contains `"cdabcdab"`, while `"abcdabcd"` does not.
Input: a = "a", b = "aa"
Output: 2
Two copies are needed to fit two characters.
Input: a = "abc", b = "wxyz"
Output: -1
`b` uses letters that never appear in `a`.

Constraints

  • `1 <= a.length, b.length <= 10^4`
  • `a` and `b` contain only lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems