Minimum Deletions to Make Character Frequencies Unique (Leetcode 1647)

Problem Link: https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/

class Solution:
    def minDeletions(self, s: str) -> int:
        # Calculate the freq of each char
        d={}
        for i in s:
            if(i not in d):
                d[i]=1
            else:
                d[i]+=1
        
        freq_set=set()
        
        # For each freq, if it is not in set,
        # add it to the set. Otherwise, remove 
        # occurances until the count is not in
        # the set. And while removing each 
        # occurance, increment the answer
        ans=0
        for i in d:
            while(d[i]>0 and d[i] in freq_set):
                d[i]-=1
                ans+=1
            
            freq_set.add(d[i])
        
        return ans 

Last updated