Ctrl K

Repeat a Word Until It Fits

Medium
You are given two strings tile and target. The string tile is written out several times in a row: for example, "abc" repeated 0 times is the empty string, once is "abc", and twice is "abcabc". Return the smallest number of repetitions of tile after which target is a substring of the result. If no number of repetitions works, return -1.
Example 1
tile="xyz", target="zxyzx" → 3

Three copies give xyzxyzxyz, which contains zxyzx starting at position 2. Two copies, xyzxyz, do not.

Example 2
tile="ab", target="ba" → 2

One copy ab does not contain ba, but abab does. The answer is 2.

Example 3
tile="abc", target="acb" → -1

However many times abc is repeated, a is always followed by b, so the answer is -1.

Constraints
  • 1 ≤ len(tile), len(target) ≤ 10⁴
  • tile and target consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHighlight Search Hits MediumHighlight Search Terms MediumCapitals Follow the Pattern MediumAnchor Near a Companion MediumWord on a Conveyor Medium
loading Python…
Input
tile="xyz", target="zxyzx"
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 146 tests on the server, hidden ones included.