Program to find maximum k-repeating substring from sequence in Python

Given a sequence of characters s, we say a string w is k-repeating if w concatenated k times is a substring of the sequence. The maximum k-repeating value of w is the highest value of k where w is k-repeating in the sequence. If w is not a substring of s, its maximum k-repeating value is 0.

Problem Statement

Given a sequence s and a string w, we need to find the maximum k-repeating value of w in the sequence.

For example, if s = "papaya" and w = "pa", the output will be 2 because "pa" repeated twice ("papa") is present in "papaya".

Algorithm

To solve this problem, we follow these steps ?

  • Count the occurrences of w in s

  • If count is 0, return 0

  • For each possible repetition from count down to 1, check if w repeated i times exists in s

  • Return the first (highest) value that works

Example

def solve(s, w):
    count = s.count(w)
    if count == 0:
        return 0
    
    for i in range(count, 0, -1):
        if w * i in s:
            return i

# Test the function
s = "papaya"
w = "pa"
print(f"Maximum k-repeating value: {solve(s, w)}")

# Let's trace through the example
print(f"\nString s: '{s}'")
print(f"Pattern w: '{w}'")
print(f"Count of '{w}' in '{s}': {s.count(w)}")
print(f"'{w}' * 2 = '{w * 2}' in '{s}': {w * 2 in s}")
print(f"'{w}' * 1 = '{w * 1}' in '{s}': {w * 1 in s}")
Maximum k-repeating value: 2

String s: 'papaya'
Pattern w: 'pa'
Count of 'pa' in 'papaya': 2
'pa' * 2 = 'papa' in 'papaya': True
'pa' * 1 = 'pa' in 'papaya': True

How It Works

The algorithm works by ?

  1. Counting occurrences: First, we count how many times w appears in s. This gives us the upper bound for possible repetitions.

  2. Checking from maximum to minimum: We start from the maximum possible repetitions and work our way down to find the highest k where w * k is a substring of s.

  3. Early termination: As soon as we find a valid k-repeating substring, we return it since we're checking from highest to lowest.

Additional Examples

def solve(s, w):
    count = s.count(w)
    if count == 0:
        return 0
    
    for i in range(count, 0, -1):
        if w * i in s:
            return i

# Test with different examples
examples = [
    ("ababc", "ab"),
    ("ababc", "ba"),
    ("ababc", "ac"),
    ("aaabaaaabaaabaaaabaaaabaaaabaaaaba", "aaaba")
]

for s, w in examples:
    result = solve(s, w)
    print(f"s='{s}', w='{w}' ? Maximum k-repeating: {result}")
s='ababc', w='ab' ? Maximum k-repeating: 2
s='ababc', w='ba' ? Maximum k-repeating: 1
s='ababc', w='ac' ? Maximum k-repeating: 0
s='aaabaaaabaaabaaaabaaaabaaaabaaaaba', w='aaaba' ? Maximum k-repeating: 5

Time Complexity

The time complexity is O(n × m × k) where ?

  • n is the length of string s

  • m is the length of pattern w

  • k is the maximum possible repetitions

The space complexity is O(m × k) for storing the concatenated pattern.

Conclusion

This algorithm efficiently finds the maximum k-repeating substring by counting occurrences first, then checking from the highest possible repetition downward. The approach ensures we find the optimal solution while avoiding unnecessary computations.

---
Updated on: 2026-03-25T20:26:19+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements