LeetCode Problem

How to Solve Minimum Deletions for At Most K Distinct Characters

To solve this problem, count the frequency of characters and remove the least frequent ones until the number of distinct characters is at most k. This uses a greedy approach where we prioritize deleting the least frequent characters. We can employ a hash table to track character counts and make efficient decisions on deletions.

GhostInterview Help

Need help with Minimum Deletions for At Most K Distinct Characters without spending extra time grinding it?

GhostInterview can read Minimum Deletions for At Most K Distinct Characters from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3545Greedy choice plus invariant validationReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Greedy choice plus invariant validation
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, count the frequency of characters and remove the least frequent ones until the number of distinct characters is at most k. This uses a greedy approach where we prioritize deleting the least frequent characters. We can employ a hash table to track character counts and make efficient decisions on deletions.

Problem Statement

You are given a string s consisting of lowercase English letters, and an integer k. Your task is to delete some characters from the string so that the resulting string contains at most k distinct characters.

Return the minimum number of deletions required to achieve this goal.

Examples

Example 1

Input: s = "abc", k = 2

Output: 1

Example 2

Input: s = "aabb", k = 2

Output: 0

Example 3

Input: s = "yyyzz", k = 1

Output: 2

Constraints

  • 1 <= s.length <= 16
  • 1 <= k <= 16
  • s consists only of lowercase English letters.

Solution Approach

Count character frequencies

The first step is to calculate the frequency of each character in the string. This can be done using a hash table where the keys are the characters and the values are their frequencies.

Sort frequencies

Once you have the character frequencies, sort them in increasing order. This allows us to prioritize deleting characters with lower frequencies, as removing them will minimize the number of deletions.

Greedily delete the least frequent characters

Starting from the least frequent characters, begin deleting characters until the number of distinct characters is reduced to k. The number of deletions is the number of removed characters.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the sorting step, which is O(n log n) where n is the number of distinct characters in the string. The space complexity is O(n) due to the hash table used to store the character frequencies.

What Interviewers Usually Probe

  • Look for the candidate's ability to break the problem down into logical steps.
  • Assess whether they can efficiently implement frequency counting and sorting.
  • Evaluate their understanding of greedy algorithms and when to apply them.

Common Pitfalls or Variants

Common pitfalls

  • Overcomplicating the problem by attempting unnecessary operations.
  • Failing to handle edge cases like when the number of distinct characters is already <= k.
  • Incorrectly deleting characters or not updating the frequency table properly.

Follow-up variants

  • Consider modifying the problem to handle uppercase letters as well.
  • Change the deletion criteria to minimize the number of characters rather than deletions.
  • Allow only a fixed number of deletions, and ask how the candidate would adjust the problem to account for this constraint.

How GhostInterview Helps

  • GhostInterview provides step-by-step guidance to help you navigate through greedy algorithms efficiently.
  • It assists in organizing the approach, especially in managing data structures like hash tables for counting frequencies.
  • The platform offers insights on optimizing solutions and avoiding common mistakes in greedy choice problems.

Topic Pages

FAQ

What is the minimum number of deletions for at most k distinct characters?

The solution involves counting the frequencies of characters and deleting the least frequent characters until there are at most k distinct characters.

How do you approach the 'Minimum Deletions for At Most K Distinct Characters' problem?

Start by counting character frequencies, sorting them, and then greedily delete the least frequent characters to reduce distinct characters to k.

What is the greedy approach used in the problem?

The greedy approach is to prioritize deleting the least frequent characters first, minimizing the number of deletions required to meet the distinct character constraint.

Why do we sort the frequencies in this problem?

Sorting the frequencies allows us to easily identify the least frequent characters, which are the best candidates for deletion.

What is the time complexity of solving this problem?

The time complexity is dominated by the sorting step, which is O(n log n), where n is the number of distinct characters in the string.

GhostInterview Solver

Need direct help with Minimum Deletions for At Most K Distinct Characters instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Deletions for At Most K Distinct Characters from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.