Minimum characters required to be removed to make frequency of each character unique | Function to find the minimum count of characters required to be deleted to make frequencies of all characters unique ; Stores frequency of each distinct character of str ; Store frequency of each distinct character such that the largest frequency is present at the top ; Stores minimum count of characters required to be deleted to make frequency of each character unique ; Traverse the string ; Update frequency of str [ i ] ; Traverse the map ; Insert current frequency into pq ; Traverse the priority_queue ; Stores topmost element of pq ; Pop the topmost element ; If pq is empty ; Return cntChar ; If frequent and topmost element of pq are equal ; If frequency of the topmost element is greater than 1 ; Insert the decremented value of frequent ; Update cntChar ; Driver Code ; Stores length of str
def minCntCharDeletionsfrequency ( str , N ) : NEW_LINE INDENT mp = { } NEW_LINE pq = [ ] NEW_LINE cntChar = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT mp [ str [ i ] ] = mp . get ( str [ i ] , 0 ) + 1 NEW_LINE DEDENT for it in mp : NEW_LINE INDENT pq . append ( mp [ it ] ) NEW_LINE DEDENT pq = sorted ( pq ) NEW_LINE while ( len ( pq ) > 0 ) : NEW_LINE INDENT frequent = pq [ - 1 ] NEW_LINE del pq [ - 1 ] NEW_LINE if ( len ( pq ) == 0 ) : NEW_LINE INDENT return cntChar NEW_LINE DEDENT if ( frequent == pq [ - 1 ] ) : NEW_LINE INDENT if ( frequent > 1 ) : NEW_LINE INDENT pq . append ( frequent - 1 ) NEW_LINE DEDENT cntChar += 1 NEW_LINE DEDENT pq = sorted ( pq ) NEW_LINE DEDENT return cntChar NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " abbbcccd " NEW_LINE N = len ( str ) NEW_LINE print ( minCntCharDeletionsfrequency ( str , N ) ) NEW_LINE DEDENT