Substring of length K having maximum frequency in the given string | ''Python3 program for the above approach ; ''Function that generates substring of length K that occurs maximum times ; '' Store the frequency of substrings ; '' Deque to maintain substrings  window size K ; '' Update the frequency of the  first subin the Map  E="".join(list(D ; '' Remove the first character of  the previous K length substring ; '' Traverse the string ; '' Insert the current character  as last character of  current substring ; '' Pop the first character of  previous K length substring ; '' Find the subthat occurs  maximum number of times  print(M) ; '' Print the substring ; ''Driver Code ; '' Given string ; '' Given K size of substring ; '' Function call
from collections import deque , Counter , defaultdict NEW_LINE import sys NEW_LINE def maximumOccurringString ( s , K ) : NEW_LINE INDENT M = { } NEW_LINE D = deque ( ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT D . append ( s [ i ] ) NEW_LINE DEDENT M [ str ( " " . join ( list ( D ) ) ) ] = M . get ( str ( " " . join ( list ( D ) ) ) , 0 ) + 1 NEW_LINE D . popleft ( ) NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT D . append ( s [ j ] ) NEW_LINE M [ str ( " " . join ( list ( D ) ) ) ] = M . get ( str ( " " . join ( list ( D ) ) ) , 0 ) + 1 NEW_LINE D . popleft ( ) NEW_LINE DEDENT maxi = - sys . maxsize - 1 NEW_LINE ans = deque ( ) NEW_LINE for it in M : NEW_LINE INDENT if ( M [ it ] >= maxi ) : NEW_LINE INDENT maxi = M [ it ] NEW_LINE ans = it NEW_LINE DEDENT DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " bbbbbaaaaabbabababa " NEW_LINE K = 5 NEW_LINE maximumOccurringString ( s , K ) NEW_LINE DEDENT