Python Forum
Python: Returning the most frequently occurring substring in a larger string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Python: Returning the most frequently occurring substring in a larger string (/thread-8196.html)



Python: Returning the most frequently occurring substring in a larger string - sskkddrit - Feb-09-2018

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.


RE: Python: Returning the most frequently occurring substring in a larger string - metulburr - Feb-09-2018

what have you tried so far?


RE: Python: Returning the most frequently occurring substring in a larger string - sskkddrit - Feb-09-2018

(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.