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.
Input: S = "abbc", N = 4, K = 2 Output: 1 In one step convert 'c' to 'a'. Hence string has two different letters a and b both occurring 2 times.
- 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 approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum characters to be replaced to make frequency of all characters same
- Minimum number of operations to move all uppercase characters before all lower case characters
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum characters required to be removed to make frequency of each character unique
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Minimum move to end operations to make all strings equal
- Minimum increment by k operations to make all elements equal
- Minimum operations to make all elements equal using the second array
- Minimum operations of given type to make all elements of a matrix equal
- Min flips of continuous characters to make all characters same in a string
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum operations to make two numbers equal
- Minimum number of given operations required to make two strings equal
- Minimum operations required to make every element greater than or equal to K
- Minimum number of operations required to make two strings equal
- Minimum increment operations to make K elements equal
- Find the number of operations required to make all array elements Equal
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum characters to be deleted from the end to make given two strings equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.