Maximum Repeating Substring
For a string sequence and a string word, return the maximum k such that word is a substring of sequence repeated k times. In other words, find the largest value of k where word repeated k times (word + word + ... + word, k times) is a substring of sequence.
Build word repeated k times for increasing k, checking if it's a substring of sequence. Stop when the repeated word is no longer found. The maximum k is the answer.
Time complexity is O(k * n) where k is the answer and n is the sequence length. Space is O(k * word.length) for building the repeated word.
Example Input & Output
'ab' repeats twice in 'ababc': 'ab' + 'ab' but only 'abab'=2
Empty string
'a' repeats 4 times: 'aaaa'
'ba' appears once
'ac' not found
Algorithm Flow

Solution Approach
Best Answers
class Solution {
public int solution(String seq, String word) {
int k=0;String r=word;
while(seq.contains(r)){k++;r+=word;}
return k;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
