Minimum number of additons to make the string balanced

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.

Examples:

Input: str = “geeksforgeeks”
Output: 15
Add 2 ‘g’, 2 ‘k’, 2 ‘s’, 3 ‘f’, 3 ‘o’ and 3 ‘r’.



Input: str = “abcd”
Output: 0
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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
#define MAX 26
  
// Function to return the minimum additions
// required to balance the given string
int minimumAddition(string str, int len)
{
  
    // To store the frequency of
    // the characters of str
    int freq[MAX] = { 0 };
  
    // Update the frequency of the characters
    for (int i = 0; i < len; i++) {
        freq[str[i] - 'a']++;
    }
  
    // To store the maximum frequency from the array
    int maxFreq = *max_element(freq, freq + MAX);
  
    // To store the minimum additions required
    int minAddition = 0;
    for (int i = 0; i < MAX; i++) {
  
        // Every character's frequency must be
        // equal to the frequency of the most
        // frequently occurring character
        if (freq[i] > 0) {
            minAddition += abs(maxFreq - freq[i]);
        }
    }
  
    return minAddition;
}
  
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int len = str.length();
  
    cout << minimumAddition(str, len);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
    final static int MAX = 26
      
    static int max_element(int freq[])
    {
        int max_ele = freq[0];
        for(int i = 0; i < MAX; i++)
        {
            if(max_ele < freq[i])
                max_ele = freq[i];
        }
        return max_ele;
    }
      
    // Function to return the minimum additions 
    // required to balance the given string 
    static int minimumAddition(String str, int len) 
    
      
        // To store the frequency of 
        // the characters of str 
        int freq[] = new int[MAX];
      
        // Update the frequency of the characters 
        for (int i = 0; i < len; i++) 
        
            freq[str.charAt(i) - 'a']++; 
        
      
        // To store the maximum frequency from the array 
        int maxFreq = max_element(freq); 
      
        // To store the minimum additions required 
        int minAddition = 0
        for (int i = 0; i < MAX; i++) 
        
      
            // Every character's frequency must be 
            // equal to the frequency of the most 
            // frequently occurring character 
            if (freq[i] > 0
            
                minAddition += Math.abs(maxFreq - freq[i]); 
            
        
        return minAddition; 
    
      
    // Driver code 
    public static void main (String[] args)
    
        String str = "geeksforgeeks"
        int len = str.length(); 
      
        System.out.println(minimumAddition(str, len)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
MAX = 26
  
# Function to return the minimum additions
# required to balance the given str1ing
def minimumAddition(str1, Len):
  
    # To store the frequency of
    # the characters of str1
    freq = [0 for i in range(MAX)]
  
    # Update the frequency of the characters
    for i in range(Len):
        freq[ord(str1[i]) - ord('a')] += 1
  
    # To store the maximum frequency from the array
    maxFreq = max(freq)
  
    # To store the minimum additions required
    minAddition = 0
    for i in range(MAX):
  
        # Every character's frequency must be
        # equal to the frequency of the most
        # frequently occurring character
        if (freq[i] > 0):
            minAddition += abs(maxFreq - freq[i])
  
    return minAddition
  
# Driver code
str1 = "geeksforgeeks"
Len = len(str1)
  
print(minimumAddition(str1, Len))
  
# This code is contributed Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG 
{
    static int MAX = 26; 
      
    static int max_element(int []freq)
    {
        int max_ele = freq[0];
        for(int i = 0; i < MAX; i++)
        {
            if(max_ele < freq[i])
                max_ele = freq[i];
        }
        return max_ele;
    }
      
    // Function to return the minimum additions 
    // required to balance the given string 
    static int minimumAddition(String str, int len) 
    
      
        // To store the frequency of 
        // the characters of str 
        int []freq = new int[MAX];
      
        // Update the frequency of the characters 
        for (int i = 0; i < len; i++) 
        
            freq[str[i] - 'a']++; 
        
      
        // To store the maximum frequency from the array 
        int maxFreq = max_element(freq); 
      
        // To store the minimum additions required 
        int minAddition = 0; 
        for (int i = 0; i < MAX; i++) 
        
      
            // Every character's frequency must be 
            // equal to the frequency of the most 
            // frequently occurring character 
            if (freq[i] > 0) 
            
                minAddition += Math.Abs(maxFreq - freq[i]); 
            
        
        return minAddition; 
    
      
    // Driver code 
    public static void Main (String[] args)
    
        String str = "geeksforgeeks"
        int len = str.Length; 
      
        Console.WriteLine(minimumAddition(str, len)); 
    
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

15


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.