#1668
Easy Algorithms Maximum repeating substring
String Dynamic Programming String Matching
41.1% acceptance
Feb 25, 2026
827
298
For a string sequence, a string word is k-repeating if word concatenated k
times is a substring of sequence. The word's maximum k-repeating value is the
highest value k where word is k-repeating.
Given strings sequence and word, return the maximum k-repeating value.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn max_repeating(sequence: String, word: String) -> i32 {
let mut k = 0;
let mut repeated = word.clone();
while sequence.contains(repeated.as_str()) {
k += 1;
repeated.push_str(&word);
}
k
}
}