Minimum Deletions to Make Character Frequencies Unique - Problem
Make Every Character Frequency Unique!

A string is considered "good" when no two different characters share the same frequency. Your mission is to find the minimum number of character deletions needed to transform any string into a good string.

What makes a string good?
โ€ข Each character must have a unique frequency
โ€ข No two different characters can appear the same number of times

Example: In string "aab", character 'a' appears 2 times and 'b' appears 1 time - this is already good! But in "aaabbbcc", both 'a' and 'b' appear 3 times, so we need deletions.

Goal: Return the minimum deletions to make the string good.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "aab"
โ€บ Output: 0
๐Ÿ’ก Note: Character 'a' appears 2 times and 'b' appears 1 time. Since all frequencies (2, 1) are already unique, no deletions are needed.
example_2.py โ€” Conflict Resolution
$ Input: s = "aaabbbcc"
โ€บ Output: 2
๐Ÿ’ก Note: Characters have frequencies: a=3, b=3, c=2. Since 'a' and 'b' both have frequency 3, we need to reduce one of them. Optimal solution: keep frequencies as [3, 2, 1], requiring 1 deletion from 'b' and 1 deletion from 'c'.
example_3.py โ€” All Same Frequency
$ Input: s = "ceabaacb"
โ€บ Output: 2
๐Ÿ’ก Note: Characters have frequencies: a=2, b=2, c=2, e=2. All characters have the same frequency! We need to make them unique: [2, 1, 0, 0], requiring 0+1+2+2 = 5 deletions. Wait, let's recalculate: optimal assignment is [2, 1, 0] for three chars, but we have 4 chars. So [2, 1, 0, and delete remaining] = 2+1+2 = 2 deletions.

Visualization

Tap to expand
๐ŸŽญ Concert Seating AnalogyGroup AWants: 3 seatsGets: 3 seats โœ“Group BWants: 3 seatsGets: 2 seats (-1)Group CWants: 2 seatsGets: 1 seat (-1)โš ๏ธ Conflict: Groups A & B both want 3 seats!Greedy Resolution Strategy1. Sort groups by desired seats: [3, 3, 2]2. Assign seats greedily: 3 โ†’ 2 โ†’ 13. Calculate people removed: (3-3) + (3-2) + (2-1) = 2Final seating: [3, 2, 1] - All unique! ๐ŸŽฏTotal people removed from groups: 2This greedy approach minimizes the total number of people who lose their seats
Understanding the Visualization
1
Count Group Sizes
Group A wants 3 seats, Group B wants 3 seats, Group C wants 2 seats
2
Identify Conflicts
Groups A and B both want 3 seats - this creates a conflict!
3
Resolve Greedily
Give Group A 3 seats, Group B gets 2 seats, Group C gets 1 seat
4
Calculate Cost
Group B loses 1 seat, Group C loses 1 seat = 2 people removed total
Key Takeaway
๐ŸŽฏ Key Insight: The greedy strategy works because we always want to preserve as many seats (characters) as possible. By assigning the highest available unique number to each group, we minimize total removals.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + k log k)

O(n) for counting frequencies, O(k log k) for sorting frequencies where k โ‰ค 26

n
2n
โšก Linearithmic
Space Complexity
O(k)

Space for frequency map and used frequencies set, where k is number of unique characters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s contains only lowercase English letters
  • Each character frequency must be unique in the final string
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
23.8K Views
Medium Frequency
~15 min Avg. Time
892 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