Given a string sequence
and a string word
, return the maximum integer k
such that word
is a substring of sequence
repeated k
times.
sequence
and word
contain non-alphabetic characters?
sequence
and word
guaranteed to be non-empty?
sequence
and word
are non-empty as per the problem constraints.sequence
and word
?
The solution involves the following steps:
k
to keep track of the maximum repeating count.repeated_word
by repeating word
k
times.repeated_word
is a substring in sequence
.k
until repeated_word
is no longer a substring of sequence
.k - 1
because the loop exits when repeated_word
is no longer a valid substring.def maxRepeating(sequence: str, word: str) -> int:
k = 0
while word * (k + 1) in sequence:
k += 1
return k
repeated_word
: This involves creating a new string which takes O(m*k) time, where m
is the length of word
.n
where n
is the length of sequence
can take O(n).Combining these, the overall time complexity in the worst case scenario would be O(k * m * n), where:
k
is the number of times until word * k
is no longer found in sequence
.m
is length of word
.n
is length of sequence
.However, in practical use, the string operations are highly optimized, making this approach efficient for typical constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?