Maximum Repeating Substring

Here’s the code that provides the solution for finding the maximum k-repeating value of a word in a given sequence:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
    def maxRepeating(self, sequence: str, word: str) -> int:
        # Initialize k as 0
        k = 0

        # Create a temporary variable to hold the repeated word
        repeated_word = word

        # Check if the repeated word is a substring of the sequence
        while repeated_word in sequence:
            # Increment k as the word is found in the sequence
            k += 1

            # Concatenate the word again for the next iteration
            repeated_word += word

        # Return the maximum k-repeating value
        return k

Explanation

  • The code initializes a counter k to keep track of the number of times the word is repeating.
  • It then uses a temporary variable repeated_word that starts with the value of word and concatenates the word again with each iteration if it’s a substring of the given sequence.
  • It continues to concatenate the word and check if it’s a substring of the sequence until it’s no longer found.
  • The code finally returns the value of k, which represents the maximum k-repeating value of the word in the given sequence.