Minimum Deletions for At Most K Distinct Characters - Problem
Minimum Deletions for At Most K Distinct Characters

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
Library Shelf Reorganization AnalogyShelf capacity: 2 genres maximum๐Ÿ“š Before: Mixed CollectionMystery: ๐Ÿ“–๐Ÿ“–๐Ÿ“–Romance: ๐Ÿ’๐Ÿ’๐Ÿ’Sci-Fi: ๐Ÿš€๐Ÿš€Horror: ๐Ÿ‘ป๐Ÿ‘ปPoetry: ๐Ÿ“3 books3 books2 books2 books1 book๐Ÿ“Š Step 1-2: Count & Rank by PopularityMystery: 3Rank #1Romance: 3Rank #2Sci-Fi: 2RemoveOthersRemoveโœ… After: Optimized Shelf (Keep Top 2)Mystery: ๐Ÿ“–๐Ÿ“–๐Ÿ“–Romance: ๐Ÿ’๐Ÿ’๐Ÿ’3 books kept3 books kept๐Ÿ“ฆ Removed Books StorageSci-Fi: ๐Ÿš€๐Ÿš€ (2) + Horror: ๐Ÿ‘ป๐Ÿ‘ป (2) + Poetry: ๐Ÿ“ (1) = 5 books removedResult: Keep 6 books, Remove 5 booksGreedy strategy: Always keep the most popular genres first!๐ŸŽฏ This minimizes the total number of books we need to remove
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.
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
24.5K Views
Medium Frequency
~12 min Avg. Time
890 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