Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to find max occurrence of a letter in a string

how can I do something like this?

string = "ABFFFBFFFFFBF"
letter = "F"

maximum occurrence of F is FFFFF so output is: 5
anyone help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is probably a more Pythonic way to do this, but this function will do the job:

def maxConsecutiveOccurrences(letter, string):
    count = 0
    maxCount = 0
    for ch in string:
        if ch == letter:
            count += 1
        else:
            count = 0
        if count > maxCount:
            maxCount = count
    return maxCount

maxConsecutiveOccurrences("F", "ABFFFBFFFFFBF") returns 5

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading