From be164090038fe848a5647ce3d28e0691d16bed78 Mon Sep 17 00:00:00 2001 From: jhaos Date: Fri, 23 Oct 2020 10:23:51 +0800 Subject: [PATCH] Add new file --- ...70\347\224\250\345\255\227\347\254\246.md" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "2020.10/10.14 - 1002. \346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" diff --git "a/2020.10/10.14 - 1002. \346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/2020.10/10.14 - 1002. \346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" new file mode 100644 index 0000000..6fdb818 --- /dev/null +++ "b/2020.10/10.14 - 1002. \346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -0,0 +1,63 @@ +给定仅有小写字母组成的字符串数组 `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]` 是小写字母 + +通过次数`19,040` | 提交次数`26,974` + +**代码实现** + +- 先统计每个字符串中的字符个数,再统计共有的字符串 + +```python +class Solution: + def commonChars(self, A: List[str]) -> List[str]: + from collections import Counter + t = [] + for i in A: + t.append(Counter(i)) + re = '' + temp = t.pop() + for i,j in temp.items(): + mins = j + for x in t: + mins = min(x[i],mins) + re+= mins*i + return list(re) +``` + +- 选取一个字符串作为基,统计该字符串中字符在各字符中的最大共有数 + +```python +class Solution: + def commonChars(self, A: List[str]) -> List[str]: + t = set(A[0]) + re = "" + for i in t: + mins = len(A[0]) + for j in A: + mins = min(mins, j.count(i)) + re += i*mins + return list(re) +``` + +``` +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/find-common-characters +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 +``` -- GitLab