Given a string str, the task is to make the string empty with the given operation. In a single operation, you can pick some characters of the string (each of the picked characters should have the same frequency) and remove them from the string. Print the total operations required to make the string empty.
Examples:
Input: str = “aabbccc”
Output: 2
In one operation, characters ‘a’ and ‘b’ can be removed since both have the same frequency.
Second operation can remove character ‘c’ having frequency 3.
Total 2 operations are required.
Input: str = “geeksforgeeks”
Output: 3
Approach:
Find unique frequencies of the characters of the string. Total count of unique frequencies will be the number of operations required to make the string empty.
For str = “aaabbbcccc”, unique frequencies are 3 and 4. Total count of unique frequencies is 2.
HashMap can be used to store the characters and their frequencies then HashSet can be used to find the count of unique frequencies which is the number of operations required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int totalOperations(string str, int len)
{
unordered_map< char , int > h;
for ( int i = 0; i < len; i++)
h[str[i]]++;
unordered_set< int > hs;
for ( auto i : h)
hs.insert(i.second);
return hs.size();
}
int main()
{
string str = "geeksforgeeks" ;
int len = str.length();
cout << totalOperations(str, len) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int totalOperations(String str, int len)
{
HashMap<Character, Integer> h = new HashMap<Character, Integer>();
for ( int i = 0 ; i < len; i++) {
if (h.containsKey(str.charAt(i)))
h.put(str.charAt(i), h.get(str.charAt(i)) + 1 );
else
h.put(str.charAt(i), 1 );
}
Set<Map.Entry<Character, Integer> > set = h.entrySet();
HashSet<Integer> hs = new HashSet<Integer>();
for (Map.Entry<Character, Integer> me : set)
hs.add(me.getValue());
return hs.size();
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int len = str.length();
System.out.println(totalOperations(str, len));
}
}
|
Python3
def totalOperations(st, length):
d = {}
for i in range (length):
if st[i] in d:
d[st[i]] + = 1
else :
d[st[i]] = 1
valueSet = set ()
for key in d.keys():
valueSet.add(d[key])
return len (valueSet)
st = "geeksforgeeks"
l = len (st)
print (totalOperations(st, l))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int totalOperations(String str, int len)
{
Dictionary< char ,
int > h = new Dictionary< char ,
int >();
for ( int i = 0; i < len; i++)
{
if (h.ContainsKey(str[i]))
h[str[i]] = h[str[i]] + 1;
else
h.Add(str[i], 1);
}
HashSet< int > hs = new HashSet< int >();
foreach (KeyValuePair< char , int > me in h)
hs.Add(me.Value);
return hs.Count;
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int len = str.Length;
Console.WriteLine(totalOperations(str, len));
}
}
|
Javascript
<script>
function totalOperations(str, len)
{
var h = new Map();
for ( var i = 0; i < len; i++)
if (h.has(str[i]))
h.set(str[i], h.get(str[i])+1)
else
h.set(str[i], 1)
var hs = new Set();
h.forEach((value, key) => {
hs.add(value);
});
return hs.size;
}
var str = "geeksforgeeks" ;
var len = str.length;
document.write( totalOperations(str, len));
</script>
|
Complexity Analysis:
- Time complexity: O(n)
- Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Sep, 2022
Like Article
Save Article