Minimum number of times to repeat a string so another string becomes a substring

Quick Overview

Given two lowercase strings a and b, find the smallest number of times a must be repeated so that b appears inside the result as a substring, or return -1 when no count works. It tests reasoning about string periodicity, bounding the search, and the cost of substring matching.

Minimum number of times to repeat a string so another string becomes a substring

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 not possible for `b` to be a substring of the repeated `a`, return `-1`. Repeating `"abc"` 0 times gives `""`, 1 time gives `"abc"`, and 2 times gives `"abcabc"`. ### Function Signature ```python def repeated_string_match(a: str, b: str) -> int: ``` ### Rules - A substring is a contiguous run of characters. - Return the smallest count `k` such that `b` occurs in `a` repeated `k` times. Return `-1` when no count works. ### Constraints - `1 <= len(a), len(b) <= 10^4` - `a` and `b` consist of lowercase English letters. ### Examples ```text Input: a = "abcd", b = "cdabcdab" Output: 3 ``` Two copies give `"abcdabcd"`, which is as long as `b` but is not equal to it. Three copies give `"abcdabcdabcd"`, and `b` starts at index 2. ```text Input: a = "a", b = "aa" Output: 2 ``` One copy is shorter than `b`. Two copies give `"aa"`, which is `b` itself. ```text Input: a = "abc", b = "acb" Output: -1 ``` In any repetition of `"abc"`, the letter after `a` is always `b`, so `"ac"` never appears and `b` cannot either.

Overview: Given two lowercase strings a and b, find the smallest number of times a must be repeated so that b appears inside the result as a substring, or return -1 when no count works. It tests reasoning about string periodicity, bounding the search, and the cost of substring matching.

Google logo
Google
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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 not possible for b to be a substring of the repeated a, return -1.

Repeating "abc" 0 times gives "", 1 time gives "abc", and 2 times gives "abcabc".

Function Signature

def repeated_string_match(a: str, b: str) -> int:

Rules

  • A substring is a contiguous run of characters.
  • Return the smallest count k such that b occurs in a repeated k times. Return -1 when no count works.

Constraints

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

Examples

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

Two copies give "abcdabcd", which is as long as b but is not equal to it. Three copies give "abcdabcdabcd", and b starts at index 2.

Input: a = "a", b = "aa"
Output: 2

One copy is shorter than b. Two copies give "aa", which is b itself.

Input: a = "abc", b = "acb"
Output: -1

In any repetition of "abc", the letter after a is always b, so "ac" never appears and b cannot either.