Minimum number of characters required to be removed such that every character occurs same number of times
Last Updated :
13 Sep, 2022
Given a string S of length N, the task is to find the minimum number of characters required to be removed such that every distinct character in the string occurs same number of times.
Examples:
Input: S = “abcbccdddd”
Output: 4
Explanation: Removing an occurrence of the characters ‘a’, ‘c’ and two occurrences of ‘d’ modifies the string to “bbccdd”. Therefore, every character in the string occurs same number of times.
Input : S = “geeksforgeeks”
Output : 5
Explanation: Removing an occurrence of ‘r’, ‘f’, ‘and ‘o’ and removing two occurrences of ‘e’ modifies S to “geeksgks”.
Approach: The idea is to use the multiset and map. Follow the steps below to solve the problem:
- Initialize a map<char, int> say countMap and a multiset<int> say countMultiset to store the frequency of every character.
- Initialize a variable say ans as INT_MAX to store the count of minimum characters to be removed.
- Traverse the string S and increment count of S[i] in countMap.
- Iterate over the map countMap and insert the frequency of the character in countMultiset.
- Find the size of multiset countMultiset and store it in a variable say m.
- Traverse the multiset countMultiset and update the ans as ans = min (ans, (N- (m-i)* (*it)))) and increment the i by 1.
- Finally, after completing the above steps print the answer as ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumDeletion(string s, int n)
{
map< char , int > countMap;
for ( int i = 0; i < n; i++) {
countMap[s[i]]++;
}
multiset< int > countMultiset;
for ( auto it : countMap) {
countMultiset.insert(it.second);
}
int ans = INT_MAX;
int i = 0;
int m = countMultiset.size();
for ( auto j : countMultiset) {
ans = min(ans, n - (m - i) * j);
i++;
}
return ans;
}
int main()
{
string S = "geeksforgeeks" ;
int N = S.length();
cout << minimumDeletion(S, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int minimumDeletion(String s, int n)
{
HashMap<Character, Integer> countMap = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
char ch = s.charAt(i);
countMap.put(ch,
countMap.getOrDefault(ch, 0 ) + 1 );
}
ArrayList<Integer> countMultiset = new ArrayList<>(
countMap.values());
Collections.sort(countMultiset);
int ans = Integer.MAX_VALUE;
int i = 0 ;
int m = countMultiset.size();
for ( int j : countMultiset)
{
ans = Math.min(ans, n - (m - i) * j);
i++;
}
return ans;
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
int N = S.length();
System.out.println(minimumDeletion(S, N));
}
}
|
Python3
import sys
def minimumDeletion(s, n):
countMap = {}
for i in s:
countMap[i] = countMap.get(i, 0 ) + 1
countMultiset = []
for it in countMap:
countMultiset.append(countMap[it])
ans = sys.maxsize + 1
i = 0
m = len (countMultiset)
for j in sorted (countMultiset):
ans = min (ans, n - (m - i) * j)
i + = 1
return ans
if __name__ = = '__main__' :
S = "geeksforgeeks"
N = len (S)
print (minimumDeletion(S, N))
|
C#
using System.Collections.Generic;
using System;
class GFG{
static int minimumDeletion( string s, int n)
{
Dictionary< char ,
int > countMap = new Dictionary< char ,
int >();
for ( int i = 0; i < n; i++)
{
if (countMap.ContainsKey(s[i]) == true )
{
countMap[s[i]] += 1;
}
else
{
countMap[s[i]] = 1;
}
}
List< int > countMultiset = new List< int >();
foreach ( var values in countMap.Values)
{
countMultiset.Add(values);
}
countMultiset.Sort();
int ans = 100000000;
int index = 0;
int m = countMultiset.Count;
foreach ( var j in countMultiset)
{
ans = Math.Min(ans, n - (m - index) * j);
index++;
}
return ans;
}
public static void Main(String[] args)
{
string S = "geeksforgeeks" ;
int N = S.Length;
Console.WriteLine(minimumDeletion(S, N));
}
}
|
Javascript
<script>
function minimumDeletion(s, n)
{
var countMap = new Map();
for ( var i = 0; i < n; i++) {
if (countMap.has(s[i]))
{
countMap.set(s[i], countMap.get(s[i])+1);
}
else
{
countMap.set(s[i], 1);
}
}
var countMultiset = [];
countMap.forEach((value, key) => {
countMultiset.push(value);
});
countMultiset.sort();
var ans = 1000000000;
var i = 0;
var m = countMultiset.length;
countMultiset.forEach(j => {
ans = Math.min(ans, n - (m - i) * j);
i++;
});
return ans;
}
var S = "geeksforgeeks" ;
var N = S.length;
document.write( minimumDeletion(S, N));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N) as using extra space for countMap and countMultiset
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...