考察了两个哈希表去交集的操作。
整体思路是统计出每个字符串里26个字符的出现的频率,然后取每个字符频率最小值,最后转成输出格式即可。
from collections import Counter
class Solution:
def commonChars(self, words: List[str]) -> List[str]:
c = Counter(words[0])
res = []
for word in words:
tmp = Counter(word)
c = c & tmp # c与tmp取交集
for ch in c:
count = c[ch]
while count:
res.append(ch)
count -= 1
return res