# Minimum Characters to be Replaced in given String to make all Characters Same

In this problem, we will find a minimum number of string characters needed to be replaced to make all characters. In the first approach, we will find the minimum count of the replaceable characters by counting the frequency of each character in the given string. In another approach, we will determine the cost to convert all string characters to a particular character and take the minimum value from that.

Problem statement – We have given a string alpha containing N alphabetical characters. We need to find the minimum number of characters to replace to make all string characters equal.

Sample examples

Input

alpha = "abca"


Output

2


Explanation – We can replace ‘b’ and ‘c’ to ‘a’ to make all characters equal.

Input

alpha = ‘aaaa’


Output

0


Explanation – We need to replace 0 characters as all characters of the string are equal.

Input

alpha = ‘abcde’


Output

4


Explanation – We need to replace any 4 characters of the string as all characters of the string are different.

## Approach 1

In this approach, we will use the map data structure to store the frequency of each character in the given string. After that, we will find the maximum frequency and get the answer by subtracting the frequency from the string length.

### Algorithm

Step 1 – Define the ‘charMap’ named' ‘map’ to map a character to an integer.

Step 2 – Traverse the string and update the character frequency in the map.

Step 3 – Initialize the ‘maxFreq’ variable with 0.

Step 4 – Make a total of 26 iterations starting from 0 to get the frequency of each character in the string. In the ‘maxFreq’ variable, store the maximum frequency of any character.

Step 5 – Return the answer after subtracting the maxFreq value from the string length.

### Example

#include <bits/stdc++.h>
using namespace std;

int getReplacableChars(string alpha){
// Map to store char frequency
map<char, int> charMap;
// store char frequency in the map
for (int p = 0; p < alpha.size(); p++) {
charMap[alpha[p]]++;
}
// Find the maximum frequency
int maxFreq = 0;
for (int p = 0; p < 26; p++) {
maxFreq = max(maxFreq, charMap['a' + p]);
}
return alpha.size() - maxFreq;
}
int main() {
string alpha = "abca";
cout << "Minimum characters we need to replace in the given string to make all characters the same is " << getReplacableChars(alpha);
return 0;
}


### Output

Minimum characters we need to replace in the given string to make all characters the same is 2


Time complexity – O(N) to calculate the frequency of characters in the string.

Space complexity – O(26) to store the frequency of each character.

## Approach 2

In this approach, we will calculate the characters needed to replace to make all characters equal to the particular character. We will calculate such costs for each alphabetical character and take minimum costs from them.

### Algorithm

Step 1 – Initialize the ‘repCost’ variable with the maximum integer value.

Step 2 – Traverse all alphabetical characters starting from ‘a’ to ‘z’.

Step 3 – Initialize the ‘charCost’ variable with 0 to store the total required replacements to make all characters of the string equal to the ‘ch’ character.

Step 4 – Traverse the string, and if any character of the string is not equal to the ‘ch’, increment the value of the ‘charCost’ by 1.

Step 5 – Update the value of the ‘repCost’ variable with the minimum value between the ‘repCost’ and ‘charCost’.

Step 6 – Return the ‘repCost’ value.

### Example

#include <bits/stdc++.h>
using namespace std;

int getReplacableChars(string alpha) {
int repCost = INT_MAX;
for (char ch = 'a'; ch <= 'z'; ch++) {
// To store the cost of making all characters equal to ch
int charCost = 0;
for (int p = 0; p < alpha.size(); p++) {
// Increase cost if character mismatch
if (alpha[p] != ch) {
charCost++;
}
}
// Store minimum cost
repCost = min(repCost, charCost);
}
// Return cost
return repCost;
}
int main() {
string alpha = "abca";
cout << "Minimum characters we need to replace in the given string to make all characters the same is " << getReplacableChars(alpha);
return 0;
}


### Output

Minimum characters we need to replace in the given string to make all characters the same is 2


Time complexity – O(N) to traverse the string.

Space complexity – O(1), as we don’t use any extra space.

We learned two approaches to solving the problem using the same logic. When we make all characters of the string equal to the particular character whose frequency is maximum, we need to make minimum replacements.

Updated on: 25-Aug-2023

51 Views