Skip to content
Related Articles

Related Articles

Minimum characters to be replaced in given String to make all characters same

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 24 Jan, 2022
View Discussion
Improve Article
Save Article

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

 

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:

  1. Store Frequencies of all characters in vector freq
  2. Find the maximum frequency mxfreq.
  3. Return (N – mxfreq) as the answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum characters
// to be replaced to make all characters
// of string str same
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;
}
 
// Driver Code
int main()
{
    char str[] = "data";
    int N = sizeof(str) / sizeof(char);
 
    cout << minCost(str, N - 1);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
   
// Function to find the minimum characters
// to be replaced to make all characters
// of string str same
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));
    }
}
 
// This code is contributed by hrithikgarg03188


Python3




# Python code for the above approach
 
# Function to find the minimum characters
# to be replaced to make all characters
# of string str same
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;
 
# Driver Code
str = "data";
N = len(str)
 
print(minCost(str, N - 1));
 
# This code is contributed by Saurabh Jaiswal


C#




// C# program for the above approach
using System;
class GFG
{
 
  // Function to find the minimum characters
  // to be replaced to make all characters
  // of string str same
  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;
  }
 
  // Driver code
  public static void Main ()
  {
 
    string str = "data";
    int N = str.Length;
    Console.WriteLine(minCost(str, N - 1));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
       // JavaScript code for the above approach
 
       // Function to find the minimum characters
       // to be replaced to make all characters
       // of string str same
       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;
       }
 
       // Driver Code
 
       let str = "data";
       let N = str.length;
 
       document.write(minCost(str, N - 1));
 
        // This code is contributed by Potta Lokesh
   </script>


 
 

Output

2

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!