Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

686. Repeated String Match

Medium35.9% Acceptance
StringString Matching
Asked by:
G
Google
ProblemSolutions (12)VideosCompanies (1)Notes

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.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #686 Repeated String Match, 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 becomes a substring of the repeated string. If it is impossible, return -1.

A practical approach is to repeatedly append A until the constructed string length becomes equal to or slightly larger than B. At this point, check whether B appears as a substring. Since the overlap between repetitions may cause B to start near the end of one copy of A and continue into the next, it is often necessary to check one or two additional repetitions.

For substring checking, you can use built-in search methods or more advanced string matching algorithms like KMP (Knuth–Morris–Pratt) for efficiency. The idea is not to generate excessive repetitions but to stop once the repeated string length clearly exceeds what is needed.

The overall complexity typically depends on substring search, giving roughly O(n + m) time with efficient matching and O(n + m) space for the constructed string.

Complexity

ApproachTime ComplexitySpace Complexity
Repeat string + substring searchO(n + m)O(n + m)
Repeat string + KMP string matchingO(n + m)O(m)

Video Solution Available

Abdul Bari

View all video solutions

Solutions (12)

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.

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).

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4int repeatedStringMatch(char * a, char * b) {
5    

Explanation

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.

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.

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

CC++JavaPythonC#JavaScript
1#include

Video Solutions

Watch expert explanations and walkthroughs

9.2 Rabin-Karp String Matching Algorithm

Abdul Bari
23:50912,392 views

Asked By Companies

1 companies
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Find the Index of the First Occurrence in a StringEasy
Shortest PalindromeHard
Repeated Substring PatternEasy
Add Bold Tag in StringMedium
More similar problems

Related Topics

StringString Matching

Problem Stats

Acceptance Rate35.9%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Repeated String Match asked in FAANG interviews?

Yes, string manipulation and substring matching problems like Repeated String Match are common in technical interviews. Companies often use them to evaluate understanding of string algorithms, edge cases, and efficient search techniques.

Why do we sometimes check one more repetition of A?

B may start near the end of one repetition of A and continue into the next repetition. Because of this overlap, simply matching lengths may miss valid cases. Adding one or two extra repetitions ensures such cross-boundary matches are detected.

What is the optimal approach for Repeated String Match?

The optimal approach is to keep repeating string A until its length becomes equal to or slightly greater than B, then check if B is a substring. Because overlaps may occur across boundaries, checking one or two additional repetitions is usually sufficient. Efficient substring search like KMP can improve performance.

What data structure or algorithm is best for Repeated String Match?

String matching algorithms such as Knuth–Morris–Pratt (KMP) or built-in substring search functions are commonly used. These methods efficiently detect whether B exists within the repeated string. The key idea is minimizing unnecessary repetitions of A.

int
m
=
strlen
(
a
)
,
n
=
strlen
(
b
)
;
6
int
repeat
=
(
n
+
m
-
1
)
/
m
,
i
;
7
char
*
result
=
(
char
*
)
malloc
(
m
*
(
repeat
+
1
)
+
1
)
;
8
result
[
0
]
=
'\0'
;
9
for
(
i
=
0
;
i
<
repeat
+
1
;
i
++
)
{
10
strcat
(
result
,
a
)
;
11
if
(
strstr
(
result
,
b
)
!=
NULL
)
{
12
free
(
result
)
;
13
return
i
+
1
;
14
}
15
}
16
free
(
result
)
;
17
return
-
1
;
18
}
19
20
int
main
(
)
{
21
char
a
[
]
=
"abcd"
;
22
char
b
[
]
=
"cdabcdab"
;
23
printf
(
"%d\n"
,
repeatedStringMatch
(
a
,
b
)
)
;
24
return
0
;
25
}
<stdio.h>
2
#
include
<string.h>
3
4
int
repeatedStringMatch
(
char
*
a
,
char
*
b
)
{
5
int
m
=
strlen
(
a
)
,
n
=
strlen
(
b
)
;
6
int
repeat
=
(
n
+
m
-
1
)
/
m
,
i
;
7
char
*
result
=
(
char
*
)
malloc
(
2
*
m
+
1
)
;
8
strcpy
(
result
,
a
)
;
9
strcat
(
result
,
a
)
;
10
for
(
i
=
0
;
i
<
repeat
;
i
++
)
{
11
if
(
strstr
(
result
+
i
*
m
,
b
)
!=
NULL
)
{
12
free
(
result
)
;
13
return
i
+
1
;
14
}
15
}
16
free
(
result
)
;
17
return
-
1
;
18
}
19
20
int
main
(
)
{
21
char
a
[
]
=
"abcd"
;
22
char
b
[
]
=
"cdabcdab"
;
23
printf
(
"%d\n"
,
repeatedStringMatch
(
a
,
b
)
)
;
24
return
0
;
25
}

Explanation

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