Minimum number of additions to make the string balanced
Last Updated :
22 Dec, 2022
Given a string str of lowercase characters, the task is to find the minimum number of characters that need to add 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 in the frequency of every character with the maximum frequency from the frequency array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 26
int minimumAddition(string str, int len)
{
int freq[MAX] = { 0 };
for ( int i = 0; i < len; i++) {
freq[str[i] - 'a' ]++;
}
int maxFreq = *max_element(freq, freq + MAX);
int minAddition = 0;
for ( int i = 0; i < MAX; i++) {
if (freq[i] > 0) {
minAddition += abs (maxFreq - freq[i]);
}
}
return minAddition;
}
int main()
{
string str = "geeksforgeeks" ;
int len = str.length();
cout << minimumAddition(str, len);
return 0;
}
|
Java
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;
}
static int minimumAddition(String str, int len)
{
int freq[] = new int [MAX];
for ( int i = 0 ; i < len; i++)
{
freq[str.charAt(i) - 'a' ]++;
}
int maxFreq = max_element(freq);
int minAddition = 0 ;
for ( int i = 0 ; i < MAX; i++)
{
if (freq[i] > 0 )
{
minAddition += Math.abs(maxFreq - freq[i]);
}
}
return minAddition;
}
public static void main (String[] args)
{
String str = "geeksforgeeks" ;
int len = str.length();
System.out.println(minimumAddition(str, len));
}
}
|
Python3
MAX = 26
def minimumAddition(str1, Len ):
freq = [ 0 for i in range ( MAX )]
for i in range ( Len ):
freq[ ord (str1[i]) - ord ( 'a' )] + = 1
maxFreq = max (freq)
minAddition = 0
for i in range ( MAX ):
if (freq[i] > 0 ):
minAddition + = abs (maxFreq - freq[i])
return minAddition
str1 = "geeksforgeeks"
Len = len (str1)
print (minimumAddition(str1, Len ))
|
C#
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;
}
static int minimumAddition(String str, int len)
{
int []freq = new int [MAX];
for ( int i = 0; i < len; i++)
{
freq[str[i] - 'a' ]++;
}
int maxFreq = max_element(freq);
int minAddition = 0;
for ( int i = 0; i < MAX; i++)
{
if (freq[i] > 0)
{
minAddition += Math.Abs(maxFreq - freq[i]);
}
}
return minAddition;
}
public static void Main (String[] args)
{
String str = "geeksforgeeks" ;
int len = str.Length;
Console.WriteLine(minimumAddition(str, len));
}
}
|
Javascript
<script>
let MAX = 26;
function max_element(freq)
{
let max_ele = freq[0];
for (let i = 0; i < MAX; i++)
{
if (max_ele < freq[i])
max_ele = freq[i];
}
return max_ele;
}
function minimumAddition(str, len)
{
let freq = new Array(MAX);
freq.fill(0);
for (let i = 0; i < len; i++)
{
freq[str[i].charCodeAt() - 'a' .charCodeAt()]++;
}
let maxFreq = max_element(freq);
let minAddition = 0;
for (let i = 0; i < MAX; i++)
{
if (freq[i] > 0)
{
minAddition += Math.abs(maxFreq - freq[i]);
}
}
return minAddition;
}
let str = "geeksforgeeks" ;
let len = str.length;
document.write(minimumAddition(str, len));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...