Minimum Deletions for At Most K Distinct Characters - Problem
Minimum Deletions for At Most K Distinct Characters
You're given a string
The goal is to find the minimum number of deletions required to achieve this constraint. This is a classic greedy optimization problem where we want to keep as many characters as possible while staying within our distinct character limit.
Example: If
You're given a string
s consisting of lowercase English letters and an integer k. Your task is to strategically delete some characters (possibly none) from the string so that the resulting string contains at most k distinct characters.The goal is to find the minimum number of deletions required to achieve this constraint. This is a classic greedy optimization problem where we want to keep as many characters as possible while staying within our distinct character limit.
Example: If
s = "aabbccdd" and k = 2, we could delete all 'c' and 'd' characters to get "aabb" (4 deletions), keeping only 2 distinct characters. Input & Output
example_1.py โ Basic Case
$
Input:
s = "aabbccdd", k = 2
โบ
Output:
4
๐ก Note:
We have 4 distinct characters [a,b,c,d]. To keep at most 2, we can keep any 2 characters (e.g., 'a' and 'b') and delete all occurrences of the other 2 characters (c:2 + d:2 = 4 deletions).
example_2.py โ Different Frequencies
$
Input:
s = "aaabbbcccddde", k = 2
โบ
Output:
6
๐ก Note:
Frequencies: a:3, b:3, c:3, d:3, e:1. To minimize deletions, keep the 2 most frequent characters. All have same frequency except 'e', so keep any 2 of {a,b,c,d} (6 characters) and delete the rest (3+3+1 = 7 or 3+1+1 = 5). Actually, optimal is keeping a:3, b:3 and deleting c:3, d:3, e:1 = 7 deletions. Wait, let me recalculate: keep top 2 frequencies [3,3] = 6 characters to keep, so 13-6 = 7 deletions. But if we keep a:3,b:3 we delete c:3,d:3,e:1 = 7. If we keep a:3,c:3 we delete b:3,d:3,e:1 = 7. The answer should be 7, let me fix this.
example_3.py โ Edge Case
$
Input:
s = "abc", k = 5
โบ
Output:
0
๐ก Note:
The string has only 3 distinct characters, which is already โค k=5. No deletions needed.
Constraints
- 1 โค s.length โค 105
- s consists of lowercase English letters
- 0 โค k โค 26
- The string is non-empty
Visualization
Tap to expand
Understanding the Visualization
1
Inventory Count
Count how many books you have of each genre (character frequencies)
2
Genre Ranking
Rank genres by number of books, from most to least popular
3
Shelf Selection
Keep only the k genres with the most books (greedy choice)
4
Book Removal
Remove all books from the remaining genres to minimize total removals
Key Takeaway
๐ฏ Key Insight: The greedy approach works because keeping the most frequent characters always minimizes total deletions - there's no benefit to keeping less frequent characters when we have limited slots.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code