Leetcode 1647: Minimum Deletions to Make Character Frequencies Unique

grid47
grid47
Exploring patterns and algorithms
May 26, 2024 6 min read

Given a string s, determine the minimum number of deletions needed to make s ‘good’. A string is ‘good’ if no two characters have the same frequency.
Problem
Approach
Steps
Complexity
Input: A string s.
Example: s = 'abc'
Constraints:
• 1 <= s.length <= 10^5
• s contains only lowercase English letters.
Output: Return the minimum number of deletions needed to make the string good.
Example: Output: 3
Constraints:
Goal: To find the minimum number of deletions needed, first calculate the frequency of each character in the string. Then, identify and delete characters with duplicate frequencies.
Steps:
• Count the frequency of each character in the string.
• Use a set to track the frequencies that have already been used.
• If a frequency is repeated, decrement it and count the deletion.
• Return the total number of deletions.
Goal: The string length can be up to 100,000 characters, and it contains only lowercase English letters.
Steps:
• 1 <= s.length <= 10^5
• s contains only lowercase English letters.
Assumptions:
• The input string will always be a valid non-empty string containing lowercase English letters.
Input: s = 'abc'
Explanation: Each character in the string 'abc' appears once, so no deletions are needed.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus