# Operations required to make the string empty

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:

`// Java implementation of the approach ` `import` `java.util.*; ` `class` `GFG { ` ` ` ` ` `// Function to return the count of operations required ` ` ` `static` `int` `totalOperations(String str, ` `int` `len) ` ` ` `{ ` ` ` ` ` `// HashMap to store characters and their frequencies ` ` ` `HashMap<Character, Integer> h = ` `new` `HashMap<Character, Integer>(); ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) { ` ` ` ` ` `// If already contains the character then ` ` ` `// increment its frequency by 1 ` ` ` `if` `(h.containsKey(str.charAt(i))) ` ` ` `h.put(str.charAt(i), h.get(str.charAt(i)) + ` `1` `); ` ` ` ` ` `// Else add the character to the HashMap with frequency 1 ` ` ` `else` ` ` `h.put(str.charAt(i), ` `1` `); ` ` ` `} ` ` ` ` ` `// Set to iterate over HashMap ` ` ` `Set<Map.Entry<Character, Integer> > set = h.entrySet(); ` ` ` ` ` `// HashSet to store unique frequency ` ` ` `HashSet<Integer> hs = ` `new` `HashSet<Integer>(); ` ` ` ` ` `// Insert frequencies into HashSet ` ` ` `for` `(Map.Entry<Character, Integer> me : set) ` ` ` `hs.add(me.getValue()); ` ` ` ` ` `// Count of unique frequencies ` ` ` `return` `hs.size(); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String str = ` `"geeksforgeeks"` `; ` ` ` `int` `len = str.length(); ` ` ` `System.out.println(totalOperations(str, len)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Minimum operations required to make all the elements distinct in an array
- Minimum number of given operations required to make two strings equal
- Find the number of operations required to make all array elements Equal
- Find the minimum number of operations required to make all array elements equal
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Minimum number of operations required to sum to binary string S
- Count of operations to make a binary string"ab" free
- Minimum swaps required to make a binary string alternating
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can become empty by recursively deleting a given sub-string
- Count the number of carry operations required to add two numbers
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum Operations to make value of all vertices of the tree Zero
- Minimal operations to make a number magical

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.