Minimum Deletions for At Most K Distinct Characters Problem
Description
LeetCode Problem 3545.
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:
Input: s = "abc", k = 2
Output: 1
Explanation:
- s has three distinct characters: 'a', 'b' and 'c', each with a frequency of 1.
- Since we can have at most k = 2 distinct characters, remove all occurrences of any one character from the string.
- For example, removing all occurrences of 'c' results in at most k distinct characters. Thus, the answer is 1.
Example 2:
Input: s = "aabb", k = 2
Output: 0
Explanation:
- s has two distinct characters ('a' and 'b') with frequencies of 2 and 2, respectively.
- Since we can have at most k = 2 distinct characters, no deletions are required. Thus, the answer is 0.
Example 3:
Input: s = "yyyzz", k = 1
Output: 2
Explanation:
- s has two distinct characters ('y' and 'z') with frequencies of 3 and 2, respectively.
- Since we can have at most k = 1 distinct character, remove all occurrences of any one character from the string.
- Removing all 'z' results in at most k distinct characters. Thus, the answer is 2.
Constraints:
1 <= s.length <= 161 <= k <= 16sconsists only of lowercase English letters.
Solution
Approach: Counting + Greedy
To keep at most k distinct letters we must delete every occurrence of all the other letters, so we want to sacrifice the letters that occur least.
Count how often each of the 26 letters occurs and sort the counts in ascending order. Letters that do not occur have count 0 and cost nothing. Keep the k largest counts and delete the rest: the answer is the sum of the first 26 - k counts.
Complexity: O(n + 26 log 26) time, which is O(n), and O(1) extra space.
class Solution:
def minDeletion(self, s: str, k: int) -> int:
return sum(sorted(Counter(s).values())[:-k])