Minimum characters to be replaced to make frequency of all characters same | C # implementation to find the 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
using System ; class GFG { static int minOperations ( String s ) { int [ ] freq = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s [ i ] - ' A ' ] ++ ; } Array . Sort ( freq ) ; Array . Reverse ( freq ) ; int answer = n ; for ( int i = 1 ; i <= 26 ; i ++ ) { if ( n % i == 0 ) { int x = n / i ; int y = 0 ; for ( int j = 0 ; j < i ; j ++ ) { y += Math . Min ( freq [ j ] , x ) ; } answer = Math . Min ( answer , n - y ) ; } } return answer ; } public static void Main ( String [ ] args ) { String s = " BBC " ; Console . Write ( minOperations ( s ) ) ; } }