Magicsheet logo

Minimum Deletions for At Most K Distinct Characters

Easy
25%
Updated 8/1/2025

Minimum Deletions for At Most K Distinct Characters

1. What is this problem about?

The Minimum Deletions for At Most K Distinct Characters problem asks for the minimum number of character deletions required to transform a given string such that it contains at most KK unique characters. You want to keep the characters that appear most frequently to minimize the number of removals.

2. Why is this asked in interviews?

This "Easy" level question is a Meta favorite. It tests a candidate's ability to use frequency counting and sorting to solve optimization problems. The Minimum Deletions for At Most K Distinct Characters interview question assesses whether you can identify that the "minimum deletions" goal is equivalent to the "maximum retentions" goal.

3. Algorithmic pattern used

The algorithmic pattern is Greedy and Counting.

  1. Count the frequency of each character in the string using a hash table or a frequency array.
  2. Sort these frequencies in descending order.
  3. Keep the top KK frequencies (the characters that appear most often).
  4. The number of deletions is (Total length of string) - (Sum of the top KK frequencies). This "Hash Table, Counting, Sorting, Greedy interview pattern" is highly efficient (O(N)O(N) to count, O(26log26)O(26 \log 26) or O(1)O(1) to sort fixed alphabet sizes).

4. Example explanation

String: "aaabbcddd", K=2K=2.

  • Frequencies: a: 3, b: 2, c: 1, d: 3.
  • Sorted frequencies: 3, 3, 2, 1.
  • To keep at most 2 distinct characters, we keep the two most frequent: 3 and 3 (characters 'a' and 'd').
  • Total kept = 6.
  • Deletions = 96=39 - 6 = 3.

5. Common mistakes candidates make

In the Minimum Deletions for At Most K Distinct Characters coding problem, a common error is trying to use a sliding window. While sliding window is used for "longest substring with K distinct characters," it is not appropriate here because you can delete characters from anywhere in the string, not just contiguous segments. Another mistake is over-complicating the logic with dynamic programming when a simple greedy count is optimal.

6. Interview preparation tip

Always distinguish between "substring" (contiguous) and "subsequence/anywhere" (non-contiguous) problems. If you can pick characters from anywhere, frequency counting is almost always the right starting point. This "Greedy and Counting interview pattern" is a fundamental skill for many string-based interview questions.

Similar Questions