题目描述
解法:哈希表计数
这道题很简单,有个稍微要注意的地方就是chars中的每个字母都只能用一次。思路:统计chars字符串中,各个字母的个数。再循环words,统计每个单词中各个字母出现的个数,查询每个出现字母的个数是否大于chars中的个数,如果大于就是错误,如果小于或等于,就是正确。
思路就是这样,主要是leetcode官方给出的答案很精简,用到了一些小技巧,值得学习借鉴(官方baba牛逼)。
我的代码:(别笑,我就是颗小白菜)
class Solution:
def countCharacters(self, words: List[str], chars: str) -> int:
if not words or not chars:
return 0
res = 0
d = {}
for i in chars:
if i not in d.keys():
d[i] = 1
else:
d[i] += 1
for i in words:
w = {}
for j in i:
if j not in