Python Forum

Full Version: Python: Returning the most frequently occurring substring in a larger string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I find the most frequently occurring sub-string in a larger string?

For example if the string 'gactctcagc' is provided, the program will return 'ctc' as it occurs twice in the string (including overlap). 'ct' and 'tc' also occur twice, but the longest string gets priority. I want to be able to account for substrings from 2-6 characters.
what have you tried so far?
(Feb-09-2018, 06:33 AM)metulburr Wrote: [ -> ]what have you tried so far?
I have found out how to find how many times 1 letter appears in a code:

def longestRepeat(x, y):
    count = 0
    longest = 0
    i = x
    while i in y:
        count+=1
        i = x*(count+1)
    return count
BUT I cannot figure out how to have it search through a given string on it's own for the most common.