Leetcode每日一题2020.2.25第1160题:拼写单词

代码

class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        chars_cnt = collections.Counter(chars)
        ans = 0
        for word in words:
            word_cnt = collections.Counter(word)
            if all([word_cnt[c] <= chars_cnt[c] for c in word]):
                ans += len(word)
            
        return ans

知识点: all函数

all()函数用于判断给定的可迭代参数 iterable 中所有元素是否都为True,如果是,返回True;否则,返回False。

猜你喜欢

转载自blog.csdn.net/m0_51210480/article/details/114087012
今日推荐