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.k = 2 distinct characters, remove all occurrences of any one character from the string.'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.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.k = 1 distinct character, remove all occurrences of any one character from the string.'z' results in at most k distinct characters. Thus, the answer is 2.Constraints:
1 <= s.length <= 161 <= k <= 16s consists only of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy explores every possible combination of deletions to find the smallest number that results in at most K unique characters. It works by systematically removing characters and checking the remaining string.
Here's how the algorithm would work step-by-step:
def minimum_deletions_brute_force(input_string, max_distinct):
string_length = len(input_string)
minimum_deletions = string_length + 1
for i in range(1 << string_length):
number_of_deletions = 0
current_string = ""
for j in range(string_length):
if (i >> j) & 1:
number_of_deletions += 1
else:
current_string += input_string[j]
# Ensure we process only valid string combinations
unique_characters = len(set(current_string))
if unique_characters <= max_distinct:
minimum_deletions = min(minimum_deletions, number_of_deletions)
if minimum_deletions == string_length + 1:
return -1
# Return the calculated minimum deletions
return minimum_deletionsThe goal is to remove the fewest letters possible from a string so that it contains at most a certain number of unique characters. We can achieve this by carefully tracking how many times each letter appears and strategically removing letters from the least frequent ones.
Here's how the algorithm would work step-by-step:
def min_deletions(input_string, max_distinct_characters):
character_counts = {}
for char in input_string:
character_counts[char] = character_counts.get(char, 0) + 1
distinct_characters_count = len(character_counts)
# No deletions are needed if we already have <= K distinct chars
if distinct_characters_count <= max_distinct_characters:
return 0
number_of_letters_removed = 0
while distinct_characters_count > max_distinct_characters:
# Find the least frequent character to remove.
least_frequent_character = None
min_count = float('inf')
for char, count in character_counts.items():
if count < min_count:
min_count = count
least_frequent_character = char
# Remove the least frequent character from consideration.
number_of_letters_removed += character_counts[least_frequent_character]
del character_counts[least_frequent_character]
# Removing a character reduces distinct character count
distinct_characters_count -= 1
return number_of_letters_removed| Case | How to Handle |
|---|---|
| Null or empty string input | Return 0, as no characters need to be deleted to have at most K distinct characters in an empty string. |
| K is zero | Return the length of the string, as all characters must be deleted to have zero distinct characters. |
| K is greater than or equal to the number of distinct characters in the string | Return 0, as no deletions are needed to satisfy the condition. |
| String contains all identical characters | Return 0, as the string already has only one distinct character (or is empty). |
| String with a very large number of distinct characters and small K (e.g., string of length 1000 with 999 distinct chars, K=1) | The sliding window approach or frequency counting should efficiently handle this, avoiding unnecessary iterations by prioritising less frequent characters for deletion |
| String contains Unicode characters | Ensure the character counting method (e.g., HashMap or array) can handle the full range of Unicode characters; the algorithm should work with any character set. |
| Very long string with many repeating sequences of different characters and a small k | The frequency counting or sliding window will need to dynamically adjust to minimize deletions and potentially have time limit issues if poorly implemented. |
| Input string contains only special characters | Algorithm should treat all characters equally; the counting mechanism handles any character as input. |