Minimum number of characters required to be removed such that every character occurs same number of times | C ++ program for the above approach ; Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Traverse the Map ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Driver Code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumDeletion ( string s , int n ) { map < char , int > countMap ; for ( int i = 0 ; i < n ; i ++ ) { countMap [ s [ i ] ] ++ ; } multiset < int > countMultiset ; for ( auto it : countMap ) { countMultiset . insert ( it . second ) ; } int ans = INT_MAX ; int i = 0 ; int m = countMultiset . size ( ) ; for ( auto j : countMultiset ) { ans = min ( ans , n - ( m - i ) * j ) ; i ++ ; } return ans ; } int main ( ) { string S = " geeksforgeeks " ; int N = S . length ( ) ; cout << minimumDeletion ( S , N ) ; return 0 ; }