Skip to content
LC-1647 Medium LeetCode

1647. Minimum Deletions to Make Character Frequencies Unique

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 61% Topics: Hash Table, String, Greedy, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

import collections
import string

class Solution(object):
    def minDeletions(self, s):
        """
        :type s: str
        :rtype: int
        """
        count = collections.Counter(s)
        result = 0
        lookup = set()
        for c in string.ascii_lowercase:
            for i in reversed(xrange(1, count[c]+1)):
                if i not in lookup:
                    lookup.add(i)
                    break
                result += 1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT