Minimum characters to be replaced to make frequency of all characters same
Last Updated :
18 May, 2021
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.
Examples:
Input: S = “ABCB”
Output: 1
Explanation:
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
Explanation:
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:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(string s)
{
int freq[26] = { 0 };
int n = s.length();
for ( int i = 0; i < n; i++) {
freq[s[i] - 'A' ]++;
}
sort(freq, freq + 26, greater< int >());
int answer = n;
for ( int i = 1; i <= 26; i++) {
if (n % i == 0) {
int x = n / i;
int y = 0;
for ( int j = 0; j < i; j++) {
y += min(freq[j], x);
}
answer = min(answer, n - y);
}
}
return answer;
}
int main()
{
string s = "BBC" ;
cout << minOperations(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minOperations(String s)
{
Integer freq[] = new Integer[ 26 ];
Arrays.fill(freq, 0 );
int n = s.length();
for ( int i = 0 ; i < n; i++) {
freq[s.charAt(i) - 'A' ]++;
}
Arrays.sort(freq, Collections.reverseOrder());
int answer = n;
for ( int i = 1 ; i <= 26 ; i++) {
if (n % i == 0 ) {
int x = n / i;
int y = 0 ;
for ( int j = 0 ; j < i; j++) {
y += Math.min(freq[j], x);
}
answer = Math.min(answer, n - y);
}
}
return answer;
}
public static void main(String[] args)
{
String s = "BBC" ;
System.out.print(minOperations(s));
}
}
|
Python3
def minOperations(s):
freq = [ 0 ] * 26
n = len (s)
for i in range (n):
freq[ ord (s[i]) - ord ( 'A' )] + = 1
freq.sort(reverse = True )
answer = n
for i in range ( 1 , 27 ):
if (n % i = = 0 ):
x = n / / i
y = 0
for j in range (i):
y + = min (freq[j], x)
answer = min (answer, n - y)
return answer
if __name__ = = "__main__" :
s = "BBC"
print (minOperations(s))
|
C#
using System;
class GFG{
static int minOperations(String s)
{
int []freq = new int [26];
int n = s.Length;
for ( int i = 0; i < n; i++)
{
freq[s[i] - 'A' ]++;
}
Array.Sort(freq);
Array.Reverse(freq);
int answer = n;
for ( int i = 1; i <= 26; i++)
{
if (n % i == 0)
{
int x = n / i;
int y = 0;
for ( int j = 0; j < i; j++)
{
y += Math.Min(freq[j], x);
}
answer = Math.Min(answer, n - y);
}
}
return answer;
}
public static void Main(String[] args)
{
String s = "BBC" ;
Console.Write(minOperations(s));
}
}
|
Javascript
<script>
function minOperations(s) {
var freq = new Array(26).fill(0);
var n = s.length;
for ( var i = 0; i < n; i++) {
freq[s[i].charCodeAt(0) -
"A" .charCodeAt(0)]++;
}
freq.sort((a, b) => b - a);
var answer = n;
for ( var i = 1; i <= 26; i++) {
if (n % i === 0) {
var x = n / i;
var y = 0;
for ( var j = 0; j < i; j++) {
y += Math.min(freq[j], x);
}
answer = Math.min(answer, n - y);
}
}
return answer;
}
var s = "BBC" ;
document.write(minOperations(s));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(26)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...