Given a string S consisting of alphabets [‘A’ – ‘Z’], the task is to find the minimum number of operations required to make frequency of every character equal. In one operation any character of the string can be chosen and replaced with another valid character.
Input: S = “ABCB”
In the given string character ‘C’ can be replaced by ‘A’, such that occurrence of every character becomes equal to 2.
Updated String = “ABAB”
Input: S = “BBC”
Output : 1
In the given string character ‘C’ can be replaced by ‘B’, such that occurrence of every character becomes equal to 3.
Updated string = “BBB”
Approach: The idea is to find the frequency of every character in the string and then sort the characters according to their frequencies in descending order. Finally, we can check for each character of string which yields the minimum number of characters to be changed and print this minimum number of characters to be changed.
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 a string concatenation of a K-length palindromic string
- Minimum characters to be replaced to remove the given substring
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum operations to make frequency of all characters equal K
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimum number of 1's to be replaced in a binary array
- Min flips of continuous characters to make all characters same in a string
- Check if a string has all characters with same frequency with one variation allowed
- Check if frequency of all characters can become same by one removal
- 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
- Modify the string such that every character gets replaced with the next character in the keyboard
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Difference between the summation of numbers whose frequency of all digits are same and different
- Largest subarray with frequency of all elements same
- Minimum digits to be removed to make either all digits or alternating digits same
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
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.