Skip to main content

Repeated String Match - Solution & Explanation

MediumStringString Matching13 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

Problem Statement

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:

Input: a = "abcd", b = "cdabcdab"
Output: 3
Explanation: We return 3 because by repeating a three times "abcdabcdabcd", b is a substring of it.

Example 2:

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

 

Constraints:

  • 1 <= a.length, b.length <= 104
  • a and b consist of lowercase English letters.

Approach Overview

Problem Overview: You are given two strings A and B. The goal is to determine the minimum number of times A must be repeated so that B appears as a substring of the repeated result. If no amount of repetition makes this possible, return -1.

Approach 1: Repeating String Approach (O((n+m) * r) time, O(n * r) space)

This approach directly simulates the process described in the problem. Keep appending A to a growing string until its length becomes at least the length of B. After each repetition, check whether B appears as a substring using standard string search. If it does, return the current repetition count. If not, append A one more time to handle cases where B overlaps across the boundary of two repetitions.

The key insight is that a valid match must appear once the repeated string length reaches or slightly exceeds B. Since substring search itself scans characters sequentially, the total cost depends on both the lengths of the strings and the number of repetitions. This solution relies heavily on basic string operations and built-in substring search.

Approach 2: String Matching Optimization with Two Repeats (O(n + m) time, O(n) space)

This optimization reduces unnecessary repetitions by calculating the minimum base repeats needed before performing substring checks. Compute k = ceil(len(B) / len(A)), which guarantees that the repeated string is at least as long as B. Build the string by repeating A exactly k times and check if B is a substring.

If the match fails, append one more A and check again. At most two checks are required because any valid occurrence of B must either fit entirely inside the first k repetitions or overlap the boundary between repetitions. This keeps the search bounded and avoids building excessively large strings. The method combines reasoning about string lengths with efficient string matching behavior.

Recommended for interviews: The optimized repeat-count approach is what interviewers expect. It shows you understand how substring alignment works across repetition boundaries. Implementing the straightforward repeating approach first demonstrates problem understanding, while limiting the checks to k and k+1 repeats demonstrates algorithmic reasoning and awareness of edge cases.

Approach 1: Repeating String Approach

This approach involves repeating the string a incrementally and checking if b becomes a substring. The minimum length of repeated a needed should be at least equal to b's length. Thus, start by repeating a just enough times to get a string longer than b and check if b is a substring. If not, repeat a one more time to cover scenarios where b spans two a's boundary.

The solution calculates the number of repetitions needed by dividing n (the length of b) by m (the length of a) rounded up. It creates a new string by repeatedly concatenating a and checks if b is a substring. If not found initially, it adds one more repetition as a final attempt.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O((m + n) * n), where m is the length of a and n is the length of b. Space Complexity: O(m + n).

Try this approach in the editor →

Approach 2: String Matching Optimization with Two Repeats

This approach relies on two repeated concatenations of string a. As per the pigeonhole principle, if b can fit within the repeated strings, it should certainly fit within two full concatenated repetitions and its prefix or suffix match.

Combines two copies of a and then checks b within different starting indices, thus quickly determining the repetition count.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(m * n). Space Complexity: O(m).

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Repeating String Approach

Time Complexity: O((m + n) * n), where m is the length of a and n is the length of b. Space Complexity: O(m + n).

String Matching Optimization with Two Repeats

Time Complexity: O(m * n). Space Complexity: O(m).

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Repeating String ApproachO((n+m) * r)O(n * r)Simple implementation when directly simulating repeated strings and substring checks.
String Matching Optimization with Two RepeatsO(n + m)O(n)Preferred interview solution. Limits repetition checks to k and k+1 using string length reasoning.

Video Solution

6 Repeated String Match Code | String Matching • Aditya Verma • 24,652 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Repeated String Match easy or hard?
Repeated String Match is rated Medium difficulty. The implementation is simple, but recognizing that only two repetition checks are required requires careful reasoning about string alignment and substring boundaries.
Repeated String Match Python/Java solution
In Python or Java, the typical solution repeats string A until its length reaches or slightly exceeds B, then uses built-in substring search like `in` in Python or `contains()` in Java. The optimized approach checks only k and k+1 repetitions.
How to solve Repeated String Match in O(n)?
Compute the minimum number of repeats needed so the repeated string length is at least len(B). Build A repeated k times and check if B is a substring. If not found, append A once more and check again. These bounded checks keep the algorithm close to linear time.
What is the best approach for Repeated String Match?
The most efficient approach calculates the minimum repetitions needed using k = ceil(len(B) / len(A)), then checks whether B appears in A repeated k or k+1 times. This limits unnecessary string building and runs in roughly O(n + m) time using standard substring search.
Is Repeated String Match asked at Google/Amazon/Meta?
Repeated String Match is a common string manipulation problem frequently seen in technical interviews. Variants involving substring search and repeated patterns have appeared in interviews at companies like Amazon, Google, and Meta.
What data structure is used in Repeated String Match?
The problem primarily uses basic string operations and substring search. No complex data structures are required, but understanding string matching behavior and boundary overlaps between repetitions is key.
What is the time complexity of Repeated String Match?
The optimized solution runs in O(n + m) time where n is the length of A and m is the length of B. Only two substring checks are needed after computing the minimum repetition count. Space complexity is O(n) for the repeated string.

Ready to solve this problem?

Practice Repeated String Match with our built-in code editor and test cases.

Practice on FleetCode