Search code examples
pythonsubstring

How to count only the continuous repetitive sub-string in a given string python


def count_overlapping(sequence, sub):
    counts = 0
    n = len(sub)
    while sub in sequence:
        counts += 1
        sequence = sequence[(sequence.find(sub) + n-1):]
    return counts

input: sequence = agatabttagataagataagatagatabagata

input: sub = agata

output: 3

This is the required output but my program is giving out 4. How do I ignore the non repetitive ones.

Someone please guide me here.


Solution

  • The simplest-but-no-so-efficient solution will be to multiply the substring each time, until you can't find it in the string anymore, and then you find the max repetitions:

    s = "agatabttagataagataagatagatabagata"
    sub = "agata"
    
    counts = 0
    while sub * (counts+1) in s:
        counts += 1
    
    print(counts)
    

    This gives 3.