686. Repeated String Match
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method Explanation
This problem asks for the minimum number of times a string a must be repeated so that another string b becomes its substring. If impossible, we should return -1.
Solution
Repeated String Match in Python
class Solution:
def repeatedStringMatch(self, a: str, b: str) -> int:
# Calculate the minimum number of repetitions for `a` to have a length
# greater than or equal to `b`.
# For example, if a="abc", b="cabcabca" (len 3, 8), we need at least
# ceil(8/3) = 3 repetitions. Integer division achieves this cleanly.
k = (len(b) + len(a) - 1) // len(a)
# Check if `b` is a substring of `a` repeated `k` times.
# The `*` operator in Python is an efficient way to repeat strings.
if b in a * k:
return k
# Check `a` repeated `k+1` times. This handles cases where `b` is
# formed by a suffix of `a` and a prefix of `a`.
# e.g., a="abcd", b="cdab". `b` is in "abcdabcd" but not "abcd".
if b in a * (k + 1):
return k + 1
# If not found in either case, it's impossible.
return -1
Repeated String Match in Java
class Solution {
public int repeatedStringMatch(String a, String b) {
// Use StringBuilder for efficient string concatenation.
StringBuilder sb = new StringBuilder();
int count = 0;
// Repeat `a` until the built string `sb` is at least as long as `b`.
while (sb.length() < b.length()) {
sb.append(a);
count++;
}
// Check if `b` is a substring of the current `sb`.
if (sb.toString().contains(b)) {
return count;
}
// Append `a` one more time and check again.
// This covers cases where `b` is formed by a suffix of `a` and a prefix of `a`.
sb.append(a);
if (sb.toString().contains(b)) {
return count + 1;
}
// If `b` is still not found, it's impossible.
return -1;
}
}
Repeated String Match in C++
class Solution {
public:
int repeatedStringMatch(string a, string b) {
// Start with one repetition of `a`.
string S = a;
int count = 1;
// Repeat `a` until the length of `S` is at least the length of `b`.
while (S.length() < b.length()) {
S += a;
count++;
}
// Check if `b` is a substring of the current `S`.
// `string::find` returns string::npos if the substring is not found.
if (S.find(b) != string::npos) {
return count;
}
// If not found, append `a` one more time and check again.
// This handles cases where `b` spans across the boundary of repetitions of `a`.
// For example, a = "abcd", b = "cdab".
S += a;
if (S.find(b) != string::npos) {
return count + 1;
}
// If `b` is still not found, it is impossible.
return -1;
}
};
Repeated String Match in TypeScript
function repeatedStringMatch(a: string, b: string): number {
// Calculate the minimum number of repetitions `k` such that
// the length of `a` repeated `k` times is at least the length of `b`.
const k = Math.ceil(b.length / a.length);
// Check if `b` is a substring of `a` repeated `k` times.
// `String.prototype.repeat()` is an efficient way to do this.
const s1 = a.repeat(k);
if (s1.includes(b)) {
return k;
}
// Check `a` repeated `k+1` times. This handles cases where `b`
// might span across the boundary of two `a`s.
// Appending `a` to `s1` is more efficient than `a.repeat(k + 1)`.
const s2 = s1 + a;
if (s2.includes(b)) {
return k + 1;
}
// If not found in either case, it's impossible.
return -1;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.