Minimum Deletions for At Most K Distinct Characters

You are given a string s consisting of lowercase English letters, and an integer k.

Your task is to delete some (possibly none) of the characters in the string so that the number of distinct characters in the resulting string is at most k.

Return the minimum number of deletions required to achieve this.

Example 1
Inputs = "abc", k = 2
Output1
s has three distinct characters, so removing all occurrences of any one character leaves at most k = 2 distinct characters and requires 1 deletion.
Example 2
Inputs = "aabb", k = 2
Output0
s already has two distinct characters, which is at most k = 2, so no deletions are required.

Constraints

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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate