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 in sequence
. If word
is not a substring of sequence
, word
's maximum k
-repeating value is 0
.
Given strings sequence
and word
, return the maximum k
-repeating value of word
in sequence
.
Example 1:
Input: sequence = "ababc", word = "ab"
Output: 2
Explanation: "abab" is a substring in "abab".
Example 2:
Input: sequence = "ababc", word = "ba"
Output: 1
Explanation: "ba" is a substring in "ababc". "baba" is not a substring in "ababc".
Example 3:
Input: sequence = "ababc", word = "ac"
Output: 0
Explanation: "ac" is not a substring in "ababc".
How would you approach solving this problem? Consider edge cases and optimize for time and space complexity.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the maximum repeating substring involves exhaustively checking all possible repetitions of the provided word within the given sequence. We'll examine every possible starting position and repetition count to see how many times the word fits consecutively. We keep track of the highest repetition count we find during this process.
Here's how the algorithm would work step-by-step:
def maximum_repeating_substring(sequence, word):
maximum_repetitions = 0
sequence_length = len(sequence)
word_length = len(word)
for start_index in range(sequence_length):
# Iterate through each possible starting position in the sequence
current_repetitions = 0
current_index = start_index
while current_index + word_length <= sequence_length:
# Check if the word matches at the current position
if sequence[current_index:current_index + word_length] == word:
current_repetitions += 1
current_index += word_length
else:
break
# Update maximum_repetitions if the current repetitions are higher
maximum_repetitions = max(maximum_repetitions, current_repetitions)
return maximum_repetitions
The key is to repeatedly search for the given word inside the larger text. Each time we find it, we increase our count. The search continues with the assumption that the substring could repeat multiple times consecutively.
Here's how the algorithm would work step-by-step:
def maximum_repeating_substring(text, word):
repeat_count = 0
repeated_word = word
# Increment the count if repeated_word is a substring of text
while repeated_word in text:
repeat_count += 1
# Build a longer string by appending the word
repeated_word += word
return repeat_count
Case | How to Handle |
---|---|
Empty sequence or word | Return 0, as no repeating substring exists in an empty string. |
Word is null or sequence is null | Throw IllegalArgumentException or return -1 to indicate invalid input, depending on the function's specification. |
Word is longer than the sequence | Return 0 because the word cannot repeat within the sequence. |
Sequence contains only the word | Return 1, as the word appears once in the sequence. |
The word is a single character and the sequence consists of the same character repeated many times. | Return the length of the sequence since the single character word fully repeats. |
The word does not appear in the sequence. | Return 0, indicating that the word has a maximum repetition of zero. |
Integer overflow if using multiplication in a naive search implementation (large sequence and word sizes). | Use a method that avoids integer multiplication, such as a sliding window or KMP algorithm, to prevent overflow. |
Sequence and word contain unicode characters | Ensure that the string comparison implementation correctly handles unicode characters by accounting for their multi-byte nature. |