Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | Function to find the maximum possible prefix of the string ; Hash map to store the frequency of the characters in the string ; Iterate over the string to find the occurence of each Character ; Minimum frequency of the Characters ; Loop to find the count of minimum frequency in the hash - map ; Loop to find the maximum possible length of the prefix in the string ; Condition to check if the frequency is greater than minimum possible freq ; maxprefix string and its length . ; Driver code ; String is initialize . ; str is passed in MaxPrefix function .
def MaxPrefix ( string ) : NEW_LINE INDENT Dict = { } NEW_LINE maxprefix = 0 NEW_LINE for i in string : NEW_LINE INDENT Dict [ i ] = Dict . get ( i , 0 ) + 1 NEW_LINE DEDENT minfrequency = min ( Dict . values ( ) ) NEW_LINE countminFrequency = 0 NEW_LINE for x in Dict : NEW_LINE INDENT if ( Dict [ x ] == minfrequency ) : NEW_LINE INDENT countminFrequency += 1 NEW_LINE DEDENT DEDENT mapper = { } NEW_LINE indi = 0 NEW_LINE for i in string : NEW_LINE INDENT mapper [ i ] = mapper . get ( i , 0 ) + 1 NEW_LINE if ( mapper [ i ] > countminFrequency ) : NEW_LINE INDENT break NEW_LINE DEDENT indi += 1 NEW_LINE DEDENT print ( string [ : indi ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = ' aabcdaab ' NEW_LINE MaxPrefix ( str ) NEW_LINE DEDENT