Minimum Deletions to Make Character Frequencies Unique - Complete Solution Guide
Minimum Deletions to Make Character Frequencies Unique is LeetCode problem 1647, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
A string s is called good if there are no two different characters in s that have the same frequency . Given a string s , return the minimum number of characters you need to delete to make s good . The frequency of a character in a string is the number of times it appears in the string. For example, in the string "aab" , the frequency of 'a' is 2 , while the frequency of 'b' is 1 . Example 1: Input: s = "aab" Output: 0 Explanation: s is already good. Example 2: Input: s = "aaabbbcc" Output: 2 Ex
Detailed Explanation
The problem asks us to find the minimum number of characters we need to delete from a given string 's' such that no two distinct characters in the resulting string have the same frequency. A string that satisfies this condition is called a 'good' string. We need to return the minimum number of deletions required to transform the input string 's' into a 'good' string. The input string 's' consists of lowercase English letters, and its length is between 1 and 10^5.
Solution Approach
The solution uses a greedy approach. First, it calculates the frequency of each character in the string 's'. Then, it iterates through the frequencies. For each frequency, it checks if that frequency has already been used. If it has, the algorithm decrements the frequency and increments the number of deletions until the frequency is unique or zero. A set is used to store the frequencies that have already been used.
Step-by-Step Algorithm
- Step 1: Create an array `freq_counts` of size 26 to store the frequency of each lowercase English letter in the string 's'. Initialize all elements of the array to 0.
- Step 2: Iterate through the string 's' and increment the corresponding element in `freq_counts` for each character.
- Step 3: Initialize a variable `deletions` to 0 to store the number of deletions required.
- Step 4: Create a set `used_frequencies` to store the frequencies that have already been used.
- Step 5: Iterate through the `freq_counts` array.
- Step 6: For each frequency `freq` in `freq_counts`, while `freq` is greater than 0 and `freq` is already in `used_frequencies`, decrement `freq` and increment `deletions`.
- Step 7: If `freq` is greater than 0 after the while loop, add `freq` to `used_frequencies`.
- Step 8: Return the value of `deletions`.
Key Insights
- Insight 1: The problem can be solved using a greedy approach. We can iterate through the frequencies of each character and reduce the frequencies until they are unique or zero.
- Insight 2: We need to keep track of the frequencies we've already used to avoid creating duplicate frequencies.
- Insight 3: Deleting characters with higher frequency first may not always be optimal. It's best to reduce frequencies one by one until uniqueness is achieved.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, String, Greedy, Sorting.
Companies
Asked at: American Express, ConsultAdd, smartnews.