Problem Description
Given a string sequence and a string word, determine the maximum integer k such that word repeated k times (i.e., word concatenated with itself k times) is a substring of sequence. If word is not found at all, the answer is 0.
Key Insights
- The problem asks us to find how many consecutive repetitions of word can be found in sequence.
- Since the length of sequence and word are small (at most 100 characters), a direct search is efficient.
- Incrementally build the repeated string (e.g., word, wordword, wordwordword, etc.) until it is no longer a substring of sequence.
- No complex data structures are needed; simple string concatenation and substring matching are sufficient.
Space and Time Complexity
Time Complexity: O(m * n) where m is the maximum k found (each check involves a substring search over sequence) and n is the length of sequence. In worst-case scenarios with small input sizes, this is efficient. Space Complexity: O(m * |word|) to store the current repeated string, which is minimal given the constraints.
Solution
We iteratively check if the word repeated k times is a substring of sequence. Start from k = 1 and increment k until the concatenated string does not appear in sequence. The last successful k is returned as the maximum repeating value. The primary data structure involved is a string that is built up iteratively, and the algorithmic technique is greedy iterative search with substring matching.