LC.1160. Find Words That Can Be Formed by Characters

在这里插入图片描述

class Solution1(object):
    def numSmallerByFrequency(self, queries, words):
        """
        直接统计
        """
        from collections import Counter
        def helper(string):
            string = sorted(list(Counter(string).items()), key=lambda x: x[0])
            return string[0][1]

        queries = list(map(helper, queries))
        words = list(map(helper, words))
        result  = []
        for q in queries:
            result.append(0)
            for w in words:
                if w > q:
                    result[-1] += 1
        return result

class Solution(object):
    def numSmallerByFrequency(self, queries, words):
        """
        排序后利用bitset 函数找到插入的位置即可
        """
        from collections import Counter
        import bisect
        def helper(string):
            string = sorted(list(Counter(string).items()), key=lambda x: x[0])
            return string[0][1]

        queries = list(map(helper, queries))
        words = list(map(helper, words))
        words.sort()
        result  = []
        for q in queries:
            index = len(words) - bisect.bisect(words, q)
            result.append(index)
        return result


class Solution(object):
    def numSmallerByFrequency(self, queries, words):
        """
        short is better
        """
        import bisect
        words  = sorted(w.count(min(w)) for w in words)
        result = [ (len(words)- bisect.bisect(words, q.count(min(q)))) for q in queries]
        return result

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值