Suppose we have a string s, s is said to be good if there are no two different characters in s that have the same frequency. We have to find the minimum number of characters we need to delete to make s a good string.
So, if the input is like s = "ssstttuu", then the output will be 2 because if we delete one 't', then there will be three 's', two 't' and two 'u', then again delete one, either 't' or 'u', to make them good.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from collections import Counter def solve(s): val = Counter(s) res = 0 numlist = sorted([i for i in val.values()]) for i in range(len(numlist)-1): if numlist[i] and numlist[i] == numlist[i+1]: numlist[i] -= 1 res += 1 k = i-1 m = i while numlist[m] and numlist[m] == numlist[k]: numlist[k] -= 1 k -= 1 m -= 1 res += 1 return res s = "ssstttuu" print(solve(s))