Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c94679

Browse files
1002 Find Common Characters.py
1 parent9d8527f commit3c94679

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

‎1002 Find Common Characters.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/python3
2+
"""
3+
Given an array A of strings made only from lowercase letters, return a list of
4+
all characters that show up in all strings within the list (including
5+
duplicates). For example, if a character occurs 3 times in all strings but not
6+
4 times, you need to include that character three times in the final answer.
7+
8+
You may return the answer in any order.
9+
10+
11+
12+
Example 1:
13+
14+
Input: ["bella","label","roller"]
15+
Output: ["e","l","l"]
16+
Example 2:
17+
18+
Input: ["cool","lock","cook"]
19+
Output: ["c","o"]
20+
21+
22+
Note:
23+
24+
1 <= A.length <= 100
25+
1 <= A[i].length <= 100
26+
A[i][j] is a lowercase letter
27+
"""
28+
importstring
29+
30+
fromtypingimportList
31+
fromcollectionsimportCounter
32+
33+
34+
classSolution:
35+
defcommonChars(self,A:List[str])->List[str]:
36+
"""
37+
running counter
38+
"""
39+
ret= []
40+
ifnotA:
41+
returnret
42+
43+
counter=Counter(A[0])
44+
forainA[1:]:
45+
cur=Counter(a)
46+
forcinstring.ascii_lowercase:
47+
counter[c]=min(counter[c],cur[c])
48+
49+
forcinstring.ascii_lowercase:
50+
ifcounter[c]>0:
51+
ret.extend([c]*counter[c])
52+
53+
returnret

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp