Minimum Deletions to Make Character Frequencies Unique - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: s = "aab"
Output: 0
💡 Note: Character frequencies: a=2, b=1. All frequencies are unique, so no deletions needed.
Example 2 — Need Deletions
$ Input: s = "aaabbbcc"
Output: 2
💡 Note: Character frequencies: a=3, b=3, c=2. We have duplicate frequency 3. Reduce one character's frequency: 3→1, requiring 2 deletions.
Example 3 — Complex Case
$ Input: s = "ceabaacb"
Output: 2
💡 Note: Character frequencies: a=3, b=2, c=2, e=1. Two characters have frequency 2. Reduce one to frequency 1, but that conflicts with e=1, so reduce to 0. Total: 2 deletions.

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters only

Visualization

Tap to expand
Minimum Deletions: "aaabbbcc" → Unique FrequenciesInput String"aaabbbcc"a=3, b=3, c=2Resolve ConflictsSort: [3,3,2]Assign: [3,2,1]→[3,2,0]Output2Deletionsa: 3→3 ✓b: 3→2 (-1)c: 2→1 (-1)Final: Frequencies [3,2,1] are all uniqueTotal Deletions: 1 + 1 = 2
Understanding the Visualization
1
Input
String with characters that may have duplicate frequencies
2
Process
Count frequencies and resolve conflicts optimally
3
Output
Minimum number of deletions needed
Key Takeaway
🎯 Key Insight: Process characters by frequency in descending order to minimize total deletions
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
89.5K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen