Minimum characters to be replaced to make frequency of all characters same | Function to find the minimum operations to convert given string to another with equal frequencies of characters ; Frequency of characters ; Loop to find the Frequency of each character ; Sort in decreasing order based on frequency ; Maximum possible answer ; Loop to find the minimum operations required such that frequency of every character is equal ; Driver Code
def minOperations ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT freq . sort ( reverse = True ) NEW_LINE answer = n NEW_LINE for i in range ( 1 , 27 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT x = n // i NEW_LINE y = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT y += min ( freq [ j ] , x ) NEW_LINE DEDENT answer = min ( answer , n - y ) NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " BBC " NEW_LINE print ( minOperations ( s ) ) NEW_LINE DEDENT