Leetcode 1668: Maximum Repeating Substring

grid47
grid47
Exploring patterns and algorithms
May 24, 2024 4 min read

You are given two strings: sequence and word. A string word is considered ‘k-repeating’ in sequence if word concatenated k times forms a substring of sequence. The task is to return the maximum value of k such that word repeated k times appears as a substring in sequence. If no such substring exists, return 0.
Problem
Approach
Steps
Complexity
Input: You are given two strings, `sequence` and `word`.
Example: sequence = 'xyzxyzxyz', word = 'xyz'
Constraints:
• 1 <= sequence.length <= 100
• 1 <= word.length <= 100
• Both `sequence` and `word` contain only lowercase English letters.
Output: Return the maximum value of `k` such that `word` repeated `k` times forms a substring of `sequence`.
Example: 3
Constraints:
• If `word` is not a substring of `sequence`, return 0.
Goal: To find the highest value of `k` where `word` repeated `k` times is a substring of `sequence`.
Steps:
• Start with the word and initialize the count of repetitions as 0.
• Keep concatenating `word` and check if the concatenated string exists in `sequence`.
• Stop when the concatenated string is no longer a substring, and return the count of repetitions.
Goal: The constraints ensure that the input sizes are manageable for direct string search operations.
Steps:
• 1 <= sequence.length <= 100
• 1 <= word.length <= 100
Assumptions:
• Both `sequence` and `word` are non-empty strings.
Input: sequence = 'xyzxyzxyz', word = 'xyz'
Explanation: The word 'xyz' can be repeated 3 times to form the substring 'xyzxyzxyz' in 'xyzxyzxyz'.

Input: sequence = 'abcdefg', word = 'xyz'
Explanation: Since 'xyz' does not exist in 'abcdefg', the maximum k-repeating value is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus