Minimum characters to be replaced in given String to make all characters same
Last Updated :
20 Sep, 2023
Given a string str of size N consisting of lowercase English characters, the task is to find the minimum characters to be replaced to make all characters of string str same. Any character can be replaced by any other character.
Example:
Input: str=”geeksforgeeks”
Output: 9
Explanation: Replace all the characters except ‘e’ of the string with ‘e’.
Input: str=”data”
Output: 2
Brute Force Approach: In this approach, we are iterating through all the possible characters from ‘a’ to ‘z’ and counting the cost to convert all the other characters to the current character. We are taking the minimum cost of all the characters and returning it as the result.
- Initialize a variable min_cost as INT_MAX.
- Traverse all possible characters ch from ‘a’ to ‘z’.
- Initialize a variable cost as 0.
- Traverse the string word from index i = 0 to N-1.
- If the character at index i is not equal to ch, increment the cost by 1.
- Update min_cost as the minimum of min_cost and cost.
- Return min_cost as the minimum characters to be replaced to make all characters of the string word the same.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost( char * word, int N)
{
int min_cost = INT_MAX;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
int cost = 0;
for ( int i = 0; i < N; i++) {
if (word[i] != ch) {
cost++;
}
}
min_cost = min(min_cost, cost);
}
return min_cost;
}
int main()
{
char str[] = "data" ;
int N = sizeof (str) / sizeof ( char );
cout << minCost(str, N - 1);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int minCost( char [] word, int N) {
int min_cost = Integer.MAX_VALUE;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
int cost = 0 ;
for ( int i = 0 ; i < N; i++) {
if (word[i] != ch) {
cost++;
}
}
min_cost = Math.min(min_cost, cost);
}
return min_cost;
}
public static void main(String[] args) {
char [] str = "data" .toCharArray();
int N = str.length;
System.out.println(minCost(str, N));
}
}
|
Python3
def min_cost(word):
min_cost = float ( 'inf' )
for ch in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
cost = 0
for char in word:
if char ! = chr (ch):
cost + = 1
min_cost = min (min_cost, cost)
return min_cost
if __name__ = = "__main__" :
word = "data"
N = len (word)
print (min_cost(word))
|
C#
using System;
class Program
{
static int MinCost( char [] word)
{
int min_cost = int .MaxValue;
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
int cost = 0;
for ( int i = 0; i < word.Length; i++)
{
if (word[i] != ch)
{
cost++;
}
}
min_cost = Math.Min(min_cost, cost);
}
return min_cost;
}
static void Main( string [] args)
{
char [] str = "data" .ToCharArray();
int N = str.Length;
Console.WriteLine(MinCost(str));
}
}
|
Javascript
function minCost(word, N) {
let min_cost = Number.MAX_SAFE_INTEGER;
for (let ch = 'a' .charCodeAt(0); ch <= 'z' .charCodeAt(0); ch++) {
let cost = 0;
for (let i = 0; i < N; i++) {
if (word[i] != String.fromCharCode(ch)) {
cost++;
}
}
min_cost = Math.min(min_cost, cost);
}
return min_cost;
}
let str = "data" ;
let N = str.length;
console.log(minCost(str, N));
|
Output:
2
Time Complexity: O(26*N)
Auxiliary Space: O(1)
Efficient Approach: The minimum number of characters to be replaced to make all characters the same is basically the number of characters not equal to the most frequent character, i.e. N – (frequency of most frequent character). Now follow the steps below to solve this problem:
- Store Frequencies of all characters in vector freq
- Find the maximum frequency mxfreq.
- Return (N – mxfreq) as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost( char * word, int N)
{
int mxfreq = 0;
vector< int > freq(26, 0);
for ( int i = 0; i < strlen (word); i++) {
freq[word[i] - 'a' ]++;
mxfreq = max(mxfreq,
freq[word[i] - 'a' ]);
}
return N - mxfreq;
}
int main()
{
char str[] = "data" ;
int N = sizeof (str) / sizeof ( char );
cout << minCost(str, N - 1);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int minCost(String word, int N)
{
int mxfreq = 0 ;
int [] freq = new int [ 26 ];
for ( int i = 0 ; i < N; i++) {
char ch = word.charAt(i);
freq[ch - 'a' ]++;
mxfreq = Math.max(mxfreq, freq[ch - 'a' ]);
}
return N - mxfreq;
}
public static void main (String[] args) {
String str = "data" ;
int N = str.length();
System.out.println(minCost(str, N - 1 ));
}
}
|
Python3
def minCost(word, N):
mxfreq = 0 ;
freq = [ 0 ] * 26
for i in range ( len (word)):
freq[ ord (word[i]) - ord ( 'a' )] = freq[ ord (word[i]) - ord ( 'a' )] + 1 ;
mxfreq = max (mxfreq, freq[ ord (word[i]) - ord ( 'a' )]);
return N - mxfreq + 1 ;
str = "data" ;
N = len ( str )
print (minCost( str , N - 1 ));
|
C#
using System;
class GFG
{
static int minCost( string word, int N)
{
int mxfreq = 0;
int [] freq = new int [26];
for ( int i = 0; i < N; i++) {
char ch = word[i];
freq[ch - 'a' ]++;
mxfreq = Math.Max(mxfreq, freq[ch - 'a' ]);
}
return N - mxfreq;
}
public static void Main ()
{
string str = "data" ;
int N = str.Length;
Console.WriteLine(minCost(str, N - 1));
}
}
|
Javascript
<script>
function minCost(word, N) {
let mxfreq = 0;
let freq = new Array(26).fill(0);
for (let i = 0; i < word.length; i++) {
freq[word[i].charCodeAt(0) - 'a' .charCodeAt(0)] =
freq[word[i].charCodeAt(0) - 'a' .charCodeAt(0)] + 1;
mxfreq = Math.max(mxfreq, freq[word[i].charCodeAt(0) -
'a' .charCodeAt(0)]);
}
return N - mxfreq + 1;
}
let str = "data" ;
let N = str.length;
document.write(minCost(str, N - 1));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...