Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using string matching strategy.
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 <= 104a and b consist of lowercase English letters.Problem summary: 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".
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: String Matching
"abcd" "cdabcdab"
"a" "aa"
repeated-substring-pattern)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #686: Repeated String Match
class Solution {
public int repeatedStringMatch(String a, String b) {
int m = a.length(), n = b.length();
int ans = (n + m - 1) / m;
StringBuilder t = new StringBuilder(a.repeat(ans));
for (int i = 0; i < 3; ++i) {
if (t.toString().contains(b)) {
return ans;
}
++ans;
t.append(a);
}
return -1;
}
}
// Accepted solution for LeetCode #686: Repeated String Match
func repeatedStringMatch(a string, b string) int {
m, n := len(a), len(b)
ans := (n + m - 1) / m
t := strings.Repeat(a, ans)
for i := 0; i < 3; i++ {
if strings.Contains(t, b) {
return ans
}
ans++
t += a
}
return -1
}
# Accepted solution for LeetCode #686: Repeated String Match
class Solution:
def repeatedStringMatch(self, a: str, b: str) -> int:
m, n = len(a), len(b)
ans = ceil(n / m)
t = [a] * ans
for _ in range(3):
if b in ''.join(t):
return ans
ans += 1
t.append(a)
return -1
// Accepted solution for LeetCode #686: Repeated String Match
struct Solution;
impl Solution {
fn repeated_string_match(a: String, b: String) -> i32 {
let mut s = String::new();
let n = a.len();
let m = b.len();
if n == 0 || m == 0 {
return -1;
}
let mut k = m / n;
if k * n < m {
k += 1;
}
for _ in 0..k {
s += &a;
}
if s.contains(&b) {
return k as i32;
}
s += &a;
if s.contains(&b) {
return (k + 1) as i32;
}
-1
}
}
#[test]
fn test() {
let a = "abcd".to_string();
let b = "cdabcdab".to_string();
assert_eq!(Solution::repeated_string_match(a, b), 3);
}
// Accepted solution for LeetCode #686: Repeated String Match
function repeatedStringMatch(a: string, b: string): number {
const m: number = a.length,
n: number = b.length;
let ans: number = Math.ceil(n / m);
let t: string = a.repeat(ans);
for (let i = 0; i < 3; i++) {
if (t.includes(b)) {
return ans;
}
ans++;
t += a;
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
At each of the n starting positions in the text, compare up to m characters with the pattern. If a mismatch occurs, shift by one and restart. Worst case (e.g., searching "aab" in "aaaa...a") checks m characters at nearly every position: O(n × m).
KMP and Z-algorithm preprocess the pattern in O(m) to build a failure/Z-array, then scan the text in O(n) — never backtracking. Total: O(n + m). Rabin-Karp uses rolling hashes for O(n + m) expected time. All beat the O(n × m) brute force of checking every position.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.