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


Suppose we have a sequence of characters called s, we say a string w is k-repeating string if w is concatenated k times is a substring of sequence. The w's maximum k-repeating value will be the highest value k where w is k-repeating in sequence. And if w is not a substring of the given sequence, w's maximum k-repeating value is 0. So if we have s and w we have to find the maximum k-repeating value of w in sequence.

So, if the input is like s = "papaya" w = "pa", then the output will be 2 as w = "pa" is present twice in "papaya".

To solve this, we will follow these steps −

  • Count:= number of w present in s

  • if Count is same as 0, then

    • return 0

  • for i in range Count to 0, decrease by 1, do

    • if i repetition of w is present in s, then

      • return i

Example (Python)

Let us see the following implementation to get better understanding −

 Live Demo

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

s = "papaya"
w = "pa"
print(solve(s, w))

Input

"papaya", "pa"

Output

2

Updated on: 17-May-2021

908 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements