A string s
is called good if there are no two different characters in s
that have the same frequency.
Given a string s
, return the minimum number of characters you need to delete to make s
good.
The frequency of a character in a string is the number of times it appears in the string. For example, in the string "aab"
, the frequency of 'a'
is 2
, while the frequency of 'b'
is 1
.
Example 1:
Input: s = "aab"
Output: 0
Explanation: s
is already good.
Example 2:
Input: s = "aaabbbcc" Output: 2 Explanation: You can delete two 'b's resulting in the good string "aaabcc". Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".
Example 3:
Input: s = "ceabaacb" Output: 2 Explanation: You can delete both 'c's resulting in the good string "eabaab". Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
Constraints:
1 <= s.length <= 105
s
contains only 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 approach to making character frequencies unique involves trying every possible combination of deletions. We generate all possible deletion scenarios and then check if the resulting string has unique character frequencies.
Here's how the algorithm would work step-by-step:
def min_deletions_brute_force(input_string):
minimum_deletions = float('inf')
string_length = len(input_string)
for number_of_deletions in range(string_length + 1):
for indices_to_delete in combinations(range(string_length), number_of_deletions):
modified_string = ''.join([char for index, char in enumerate(input_string) if index not in indices_to_delete])
frequencies = {}
for char in modified_string:
frequencies[char] = frequencies.get(char, 0) + 1
if len(set(frequencies.values())) == len(frequencies):
# Check if frequencies are unique.
minimum_deletions = min(minimum_deletions, number_of_deletions)
return minimum_deletions
from itertools import combinations
The goal is to find the fewest removals to make all character counts different. The clever strategy is to count each character, then gradually reduce counts if needed, deleting only what's required to achieve uniqueness.
Here's how the algorithm would work step-by-step:
def minimum_deletions_to_make_character_frequencies_unique(input_string):
character_counts = {}
for char in input_string:
character_counts[char] = character_counts.get(char, 0) + 1
frequencies = sorted(character_counts.values(), reverse=True)
deletion_count = 0
# Iterate through the frequencies to ensure uniqueness.
for i in range(len(frequencies)):
for j in range(i + 1, len(frequencies)):
# Reduce current frequency if it's not unique.
while frequencies[i] != 0 and frequencies[i] == frequencies[j]:
frequencies[i] -= 1
deletion_count += 1
#Re-sort to maintain order after decrement.
frequencies.sort(reverse=True)
break
return deletion_count
Case | How to Handle |
---|---|
Empty input string | Return 0 as no deletions are needed for an empty string. |
String with a single character | Return 0 as a single character string has unique frequency. |
String where all characters are the same (e.g., 'aaaa') | The algorithm should iteratively decrement the frequency until it becomes unique, accumulating deletions. |
String with already unique character frequencies (e.g., 'abc') | Return 0 as no deletions are needed. |
Maximum length string (considering memory constraints and potential integer overflow when counting frequencies) | Use appropriate data types (e.g., int or long) to prevent overflow and ensure the frequency array can handle the length. |
String with a large number of distinct characters each with a high frequency | The algorithm should handle numerous reductions in frequency without exceeding time limit (optimize to avoid O(n^2)). |
String where the frequencies of some characters are already zero before any deletions | The algorithm should still correctly process other characters, treating the zero frequencies as unique and not requiring deletion. |
String requiring many deletions to achieve unique frequencies | Ensure the algorithm does not time out due to excessive iterations to achieve unique frequencies. |