LeetCode Problem

How to Solve Repeated String Match

To solve the Repeated String Match problem, repeat string a until b becomes a substring of the repeated string. The challenge is determining the minimum number of repetitions. Efficient string matching ensures optimal performance, and recognizing failure modes can help optimize the solution to avoid unnecessary calculations.

GhostInterview Help

Need help with Repeated String Match without spending extra time grinding it?

GhostInterview can read Repeated String Match from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #686String plus String MatchingReviewed 2026-03-08
Difficulty
Medium
Primary pattern
String plus String Matching
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the Repeated String Match problem, repeat string a until b becomes a substring of the repeated string. The challenge is determining the minimum number of repetitions. Efficient string matching ensures optimal performance, and recognizing failure modes can help optimize the solution to avoid unnecessary calculations.

Problem Statement

Given two strings a and b, your task is to determine the minimum number of times you should repeat string a so that string b becomes a substring of the repeated string. If it is not possible to make b a substring even after repeating a, return -1.

For example, consider the string a = "abcd" and b = "cdabcdab". Repeating string a three times results in "abcdabcdabcd", where b becomes a substring. If b cannot be made a substring after multiple repetitions, the output should be -1.

Examples

Example 1

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

Output: 3

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

Example details omitted.

Constraints

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

Solution Approach

Initial String Matching

Start by determining the minimum number of repetitions required for the length of a to exceed the length of b. A basic approach is to check if b is already a substring of a.

Repeated String Expansion

To handle edge cases, repeat a enough times so its length surpasses b's length. Then, check if b appears as a substring within the expanded version of a.

Efficient Check and Return

If b is found in the repeated string, return the number of repetitions. If not, return -1. The time complexity is O(M+N), where M is the length of a and N is the length of b.

Complexity Analysis

MetricValue
TimeO(M+N)
SpaceO(1)

The time complexity is O(M+N) due to the string matching operation, where M is the length of string a and N is the length of string b. Space complexity is O(1) as we do not use any additional storage apart from the input strings.

What Interviewers Usually Probe

  • Ability to handle string manipulations and substring matching efficiently.
  • Proficiency in optimizing solutions for string-based problems with variable input sizes.
  • Understanding of complexity trade-offs in terms of time and space for string matching problems.

Common Pitfalls or Variants

Common pitfalls

  • Not considering edge cases where b might be smaller than a or when it might take multiple repetitions to match.
  • Misunderstanding the failure condition when it is impossible for b to be a substring of repeated a.
  • Improper handling of large input sizes, leading to performance inefficiencies.

Follow-up variants

  • Modify the problem by restricting the maximum number of repetitions.
  • Allow for case-sensitive string matching to increase complexity.
  • Test with random large inputs to assess time performance more accurately.

How GhostInterview Helps

  • GhostInterview provides optimal solution hints and step-by-step reasoning for efficient string matching.
  • It helps identify common pitfalls and failure modes, guiding interviewees to avoid unnecessary computations.
  • By simulating real interview conditions, GhostInterview helps prepare you for handling such problems under time constraints.

Topic Pages

FAQ

What is the time complexity of the Repeated String Match problem?

The time complexity is O(M+N), where M is the length of string a and N is the length of string b.

How do I handle edge cases in Repeated String Match?

Make sure to account for situations where b is smaller than a or cannot be made a substring after multiple repetitions.

Can Repeated String Match be solved without repeating string a?

No, the solution requires repeating string a at least a few times to check if b becomes a substring.

What is the pattern for solving the Repeated String Match problem?

The pattern involves string matching, expanding the repeated string, and checking if b is a substring of the repeated a.

What happens if b cannot be made a substring of repeated a?

If b cannot be a substring after repeating a enough times, the function should return -1.

GhostInterview Solver

Need direct help with Repeated String Match instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Repeated String Match from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.