代码
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。