1002. 查找常用字符

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。

你可以按任意顺序返回答案。

示例 1:

输入:["bella","label","roller"]
输出:["e","l","l"]
示例 2:

输入:["cool","lock","cook"]
输出:["c","o"]
 

提示:

1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j] 是小写字母

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-common-characters

class Solution:
    def commonChars(self, A: List[str]) -> List[str]:
        s=''
        for a in A:
            s+=a
        cnt=collections.Counter(list(s))
        for a in A:
            cnter=collections.Counter(list(a))
            for i in cnt:
                cnt[i]=min(cnt[i],cnter[i])
        res=[]
        for i in cnt: 
          res+=i*cnt[i]
        return res

提取核心思想优化=>

class Solution:
    def commonChars(self, A: List[str]) -> List[str]:
        if not A:return []
        res=[]
        key=set(A[0])
        for k in key:
            cnt=min(a.count(k) for a in A)
            res+=k*cnt
        return res
原文地址:https://www.cnblogs.com/xxxsans/p/13799135.html