Minimum characters required to be removed to make frequency of each character unique | C ++ program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCntCharDeletionsfrequency ( string & str , int N ) { unordered_map < char , int > mp ; priority_queue < int > pq ; int cntChar = 0 ; for ( int i = 0 ; i < N ; i ++ ) { mp [ str [ i ] ] ++ ; } for ( auto it : mp ) { pq . push ( it . second ) ; } while ( ! pq . empty ( ) ) { int frequent = pq . top ( ) ; pq . pop ( ) ; if ( pq . empty ( ) ) { return cntChar ; } if ( frequent == pq . top ( ) ) { if ( frequent > 1 ) { pq . push ( frequent - 1 ) ; } cntChar ++ ; } } return cntChar ; } int main ( ) { string str = " abbbcccd " ; int N = str . length ( ) ; cout << minCntCharDeletionsfrequency ( str , N ) ; return 0 ; }