array and hashmap
December 30, 2022
Remove letter to equalize frequency
We will count the frequency of each letter. Then we will find the minimum and maximum frequency. If the minimum frequency is equal to the maximum frequency, then we can remove any letter. Otherwise, we will remove the letter with the maximum frequency.
class Solution:
def equalFrequency(self, word: str) -> bool:
count = collections.Counter(word)
for ch in word:
count[ch] -= 1
if count[ch] == 0:
count.pop(ch)
if len(set(count.values())) == 1:
return True
count[ch] += 1
return False
Time complexity: O(n)
Space complexity: O(n)