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
function minOperations ( s ) { var freq = new Array ( 26 ) . fill ( 0 ) ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } freq . sort ( ( a , b ) => b - a ) ; var answer = n ; for ( var i = 1 ; i <= 26 ; i ++ ) { if ( n % i === 0 ) { var x = n / i ; var y = 0 ; for ( var j = 0 ; j < i ; j ++ ) { y += Math . min ( freq [ j ] , x ) ; } answer = Math . min ( answer , n - y ) ; } } return answer ; } var s = " " ; document . write ( minOperations ( s ) ) ;