Given a string str of lowercase characters, the task is to find the minimum number of characters that need to added to the string in order to make it balanced. A string is said to be balanced if and only if the number of occurrences of each of the characters is equal.
Input: str = “geeksforgeeks”
Add 2 ‘g’, 2 ‘k’, 2 ‘s’, 3 ‘f’, 3 ‘o’ and 3 ‘r’.
Input: str = “abcd”
The string is already balanced.
Approach: In order to minimize the additions required, every character’s frequency must be made equal to the frequency of the element occurring most frequently. So first, create a frequency array and find the frequency of all the characters of the given string. Now, the required answer will be the sum of the absolute differences of the frequency of every character with the maximum frequency from the frequency array.
Below is the implementation of the above approach:
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of swaps to make two binary string equal
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of Appends needed to make a string palindrome
- Minimum number of characters to be removed to make a binary string alternate
- Minimum changes required to make first string substring of second string
- Number of ways to partition a string into two balanced subsequences
- Number of balanced bracket expressions that can be formed from a string
- Minimum changes to a string to make all substrings distinct
- Minimum cost to make a string free of a subsequence
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum operations required to make the string satisfy the given condition
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.