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
Goal: Return the minimum deletions to make the string good.
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
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
โก Linearithmic
Space Complexity
O(k)
Space for frequency map and used frequencies set, where k is number of unique characters
โ Linear Space
Constraints
- 1 โค s.length โค 105
- s contains only lowercase English letters
- Each character frequency must be unique in the final string
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code