• leetcode - 1647. Minimum Deletions to Make Character Frequencies Unique


    Description

    A string s is called good if there are no two different characters in s that have the same frequency.

    Given a string s, return the minimum number of characters you need to delete to make s good.

    The frequency of a character in a string is the number of times it appears in the string. For example, in the string “aab”, the frequency of ‘a’ is 2, while the frequency of ‘b’ is 1.

    Example 1:

    Input: s = "aab"
    Output: 0
    Explanation: s is already good.
    
    • 1
    • 2
    • 3

    Example 2:

    Input: s = "aaabbbcc"
    Output: 2
    Explanation: You can delete two 'b's resulting in the good string "aaabcc".
    Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".
    
    • 1
    • 2
    • 3
    • 4

    Example 3:

    Input: s = "ceabaacb"
    Output: 2
    Explanation: You can delete both 'c's resulting in the good string "eabaab".
    Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
    
    • 1
    • 2
    • 3
    • 4

    Constraints:

    1 <= s.length <= 10^5
    s contains only lowercase English letters.
    
    • 1
    • 2

    Solution

    Greedy, first delete the most frequent character, delete until the frequency is unique.

    Time complexity: o ( n ) o(n) o(n)?
    Space complexity: o ( n ) o(n) o(n)

    Code

    class Solution:
        def minDeletions(self, s: str) -> int:
            fre_cnt = {}
            for c in s:
                fre_cnt[c] = fre_cnt.get(c, 0) + 1
            res = 0
            need_delete = []
            exists = []
            for c, v in fre_cnt.items():
                if v in exists:
                    need_delete.append(v)
                else:
                    exists.append(v)
            exists.sort()
            need_delete.sort()
            for i in need_delete:
                new_i = i - 1
                while new_i > 0 and bisect.bisect_left(exists, new_i) < len(exists) and exists[bisect.bisect_left(exists, new_i)] == new_i:
                    new_i -= 1
                new_i_index = bisect.bisect_left(exists, new_i)
                exists.insert(new_i_index, new_i)
                res += i - new_i
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    Or a simpler one:

    class Solution:
        def minDeletions(self, s: str) -> int:
            fre_cnt = {}
            for c in s:
                fre_cnt[c] = fre_cnt.get(c, 0) + 1
            res = 0
            exists = set()
            for c, v in fre_cnt.items():
                while v > 0 and v in exists:
                    res += 1
                    v -= 1
                exists.add(v)
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    利用人工智能和大数据技术,优化IT运维流程和策略
    【D题解题思路】2023年第九届数维杯国际大学生数学建模挑战赛(文末领取方式)
    华为OD机考算法题:食堂供餐
    Python 进程和线程详解(multiprocessing、threading)
    AirTest
    Redis连接不上的报错解决方案汇总
    【软件测试】测试中的风险有哪些?
    【HDU No. 2586】 树上距离 How far away ?
    Python大数据之pandas快速入门(二)
    线性代数模型—python求解
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/132844190