方法一:
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)
for c in word_cnt:
if chars_cnt[c] < word_cnt[c]:
break
else:
ans += len(word)
return ans
方法二:
class Solution:
def countCharacters(self, words: List[str], chars: str) -> int:
ans = 0
for w in words:
for i in w:
if w.count(i) > chars.count(i):
break
else:
ans+=len(w)
return ans