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

• Difficulty Level : Basic
• Last Updated : 24 Jan, 2022

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 ` `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``;`   `    ``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

 ``

Output

`2`

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :