Minimum number of characters required to be removed such that every character occurs same number of times | Python3 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
import sys NEW_LINE def minimumDeletion ( s , n ) : NEW_LINE INDENT countMap = { } NEW_LINE for i in s : NEW_LINE INDENT countMap [ i ] = countMap . get ( i , 0 ) + 1 NEW_LINE DEDENT countMultiset = [ ] NEW_LINE for it in countMap : NEW_LINE INDENT countMultiset . append ( countMap [ it ] ) NEW_LINE DEDENT ans = sys . maxsize + 1 NEW_LINE i = 0 NEW_LINE m = len ( countMultiset ) NEW_LINE for j in sorted ( countMultiset ) : NEW_LINE INDENT ans = min ( ans , n - ( m - i ) * j ) NEW_LINE i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE N = len ( S ) NEW_LINE print ( minimumDeletion ( S , N ) ) NEW_LINE DEDENT