Largest sub | Python3 implementation of the approach ; Function that return true if frequency of all the present characters is at least k ; If the character is present and its frequency is less than k ; Function to set every frequency to zero ; Function to return the length of the longest sub - string such that it contains every character at least k times ; To store the required maximum length ; Starting index of the sub - string ; Ending index of the sub - string ; If the frequency of every character of the current sub - string is at least k ; Update the maximum possible length ; Driver code
MAX = 26 NEW_LINE def atLeastK ( freq , k ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] != 0 and freq [ i ] < k ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def setZero ( freq ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT freq [ i ] = 0 ; NEW_LINE DEDENT DEDENT def findlength ( string , n , k ) : NEW_LINE INDENT maxLen = 0 ; NEW_LINE freq = [ 0 ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT setZero ( freq ) ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT freq [ ord ( string [ j ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE if ( atLeastK ( freq , k ) ) : NEW_LINE INDENT maxLen = max ( maxLen , j - i + 1 ) ; NEW_LINE DEDENT DEDENT DEDENT return maxLen ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " xyxyyz " ; NEW_LINE n = len ( string ) ; NEW_LINE k = 2 ; NEW_LINE print ( findlength ( string , n , k ) ) ; NEW_LINE DEDENT