You are given a 0-indexed string word
, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word
so that the frequency of every letter present in word
is equal.
Return true
if it is possible to remove one letter so that the frequency of all letters in word
are equal, and false
otherwise.
Note:
x
is the number of times it occurs in the string.Example 1:
Input: word = "abcc"
Output: true
Explanation: Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.
Example 2:
Input: word = "aazz"
Output: false
Explanation: We must delete a character, so either the frequency of "a" is 1 and the frequency of "z" is 2, or vice versa. It is impossible to make all present letters have equal frequency.
Constraints:
2 <= word.length <= 100
word
consists of lowercase English letters only.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 solving this problem is simple: we'll try removing each letter one by one and then check if the resulting word meets the desired condition. If it does, we've found a solution. If none of the letters satisfy the condition when removed, then the answer is false.
Here's how the algorithm would work step-by-step:
def remove_letter_to_equalize_frequency(word):
for index_to_remove in range(len(word)):
# Create a new string with the character at index removed
modified_word = word[:index_to_remove] + word[index_to_remove+1:]
# Count the frequency of each character in the modified word
frequency_counts = {}
for char in modified_word:
frequency_counts[char] = frequency_counts.get(char, 0) + 1
# If the modified word is empty, return True
if not frequency_counts:
return True
# Check if all the frequencies are equal
first_frequency = list(frequency_counts.values())[0]
# Early exit if freqs are not the same
frequencies_are_equal = all(frequency == first_frequency for frequency in frequency_counts.values())
if frequencies_are_equal:
return True
# If no removal results in equal frequencies, return False
return False
To equalize the frequency of all characters by removing just one, the best approach is to analyze the frequency of each character in the string. We need to check if removing a single instance of any character can make the frequency of the remaining characters the same.
Here's how the algorithm would work step-by-step:
def remove_letter_to_equalize_frequency(input_string):
character_counts = {}
for char in input_string:
character_counts[char] = character_counts.get(char, 0) + 1
unique_characters = list(character_counts.keys())
for char_to_remove in unique_characters:
temp_counts = character_counts.copy()
# Simulate removing one instance of the character.
temp_counts[char_to_remove] -= 1
if temp_counts[char_to_remove] == 0:
del temp_counts[char_to_remove]
if not temp_counts:
return True
frequencies = list(temp_counts.values())
# Check if all remaining characters have the same frequency.
if len(set(frequencies)) == 1:
return True
return False
Case | How to Handle |
---|---|
Empty string | Return true immediately, as an empty string vacuously satisfies the condition since there are no characters to have unequal frequency. |
String with a single character | Return true, as removing the only character will result in an empty string with equal frequencies. |
String with all characters the same | Return true because removing any single character results in all remaining characters having the same frequency. |
Maximum length string with many unique characters | The solution should use a frequency counting data structure (e.g., hash map) to efficiently handle large inputs without exceeding time limits. |
String with very high frequency of one character and low frequencies of others | The solution must correctly identify that removing one of the high-frequency characters could potentially equalize the frequencies. |
String where removing a character leads to multiple possible equalized frequencies | The problem statement implies only one valid removal needs to be found so the algorithm only needs to return true once a valid removal is found. |
String where no character removal can equalize frequencies | The algorithm should return false if after checking all possible removals, no equal frequency state is achieved. |
String with characters beyond ASCII (Unicode characters) | Ensure the frequency counting data structure supports Unicode characters and their respective frequency counts. |