# Minimum operations to make frequency of all characters equal K

Given a string S of length N. The task is to find the minimum number of steps required on strings, so that it has exactly **K** different alphabets all with the same frequency.

**Note**: In one step, we can change a letter to any other letter.

**Examples:**

Input: S = "abbc", N = 4, K = 2Output: 1 In one step convert 'c' to 'a'. Hence string has two different letters a and b both occurring 2 times.

**Approach**:

- Check if K divides N, then only it is possible to convert the given string, otherwise not.
- Maintain the count of all alphabets present in string S, in an array A.
- Evaluate
**E = N/K**, the frequency with which alphabets will be present in the final string. - Separate the alphabets with frequency more than or equal to E and less than E in two parts.
- Maintain the number of steps required for each alphabet to convert its count to E, sort these vector obtained in above step.
- Lastly, take all possibility to pick:
Set 1 : 0 Set 2 : K Set 1 : 1 Set 2 : K-1 .... so on

- Keep a
*ans*variable to calculate minimum number of steps among all possibility in step 6. - Say L1 is the number of operation required on Set 1, L2 is the number of operations required on set 2. Then total operations required is maximum of L1, L2 . As suppose ‘a’ is required one less in string while ‘b’ is required one more than we can change ‘a’ to ‘b’, thus reducing number of steps.

Below is the implementation of the above appraoch:

## C++

`// C++ program to convert the given string ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum number of ` `// operations to convert the given string ` `void` `minOperation(string S, ` `int` `N, ` `int` `K) ` `{ ` ` ` `// Check if N is divisible by K ` ` ` `if` `(N % K) { ` ` ` `cout << ` `"Not Possible"` `<< endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Array to store frequency of characters ` ` ` `// in given string ` ` ` `int` `count[26] = { 0 }; ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `count[S[i] - 97]++; ` ` ` `} ` ` ` ` ` `int` `E = N / K; ` ` ` ` ` `vector<` `int` `> greaterE; ` ` ` `vector<` `int` `> lessE; ` ` ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` ` ` `// Two arrays with number of operations ` ` ` `// required ` ` ` `if` `(count[i] < E) ` ` ` `lessE.push_back(E - count[i]); ` ` ` `else` ` ` `greaterE.push_back(count[i] - E); ` ` ` `} ` ` ` ` ` `sort(greaterE.begin(), greaterE.end()); ` ` ` `sort(lessE.begin(), lessE.end()); ` ` ` ` ` `int` `mi = INT_MAX; ` ` ` ` ` `for` `(` `int` `i = 0; i <= K; i++) { ` ` ` ` ` `// Checking for all possibility ` ` ` `int` `set1 = i; ` ` ` `int` `set2 = K - i; ` ` ` ` ` `if` `(greaterE.size() >= set1 && lessE.size() >= set2) { ` ` ` ` ` `int` `step1 = 0; ` ` ` `int` `step2 = 0; ` ` ` ` ` `for` `(` `int` `j = 0; j < set1; j++) ` ` ` `step1 += greaterE[j]; ` ` ` ` ` `for` `(` `int` `j = 0; j < set2; j++) ` ` ` `step2 += lessE[j]; ` ` ` ` ` `mi = min(mi, max(step1, step2)); ` ` ` `} ` ` ` `} ` ` ` ` ` `cout << mi << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string S = ` `"accb"` `; ` ` ` `int` `N = S.size(); ` ` ` `int` `K = 2; ` ` ` ` ` `minOperation(S, N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to convert the given string

# Function to find the minimum number of

# operations to convert the given string

def minOperation(S, N, K):

# Check if N is divisible by K

if N % K:

print(“Not Possible”)

return

# Array to store frequency of

# characters in given string

count = [0] * 26

for i in range(0, N):

count[ord(S[i]) – 97] += 1

E = N // K

greaterE = []

lessE = []

for i in range(0, 26):

# Two arrays with number of

# operations required

if count[i] < E:
lessE.append(E - count[i])
else:
greaterE.append(count[i] - E)
greaterE.sort()
lessE.sort()
mi = float('inf')
for i in range(0, K + 1):
# Checking for all possibility
set1, set2 = i, K - i
if (len(greaterE) >= set1 and

len(lessE) >= set2):

step1, step2 = 0, 0

for j in range(0, set1):

step1 += greaterE[j]

for j in range(0, set2):

step2 += lessE[j]

mi = min(mi, max(step1, step2))

print(mi)

# Driver Code

if __name__ == “__main__”:

S = “accb”

N = len(S)

K = 2

minOperation(S, N, K)

# This code is contributed by Rituraj Jain

**Output:**

1

## Recommended Posts:

- Minimum increment by k operations to make all elements equal
- Minimum move to end operations to make all strings equal
- Minimum number of given operations required to make two strings equal
- Minimum operations of given type to make all elements of a matrix equal
- Minimum number of operations to move all uppercase characters before all lower case characters
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Find the number of operations required to make all array elements Equal
- Minimum operations to make GCD of array a multiple of k
- Minimum no. of operations required to make all Array Elements Zero
- Find minimum operations needed to make an Array beautiful
- Minimum characters to be added at front to make string palindrome
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum number of characters to be removed to make a binary string alternate
- Minimum replacements to make adjacent characters unequal in a ternary string
- Make array elements equal in Minimum Steps

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.