Skip to content

Commit 63a62c1

Browse files
committed
Solution for problem number 1002
1 parent e905659 commit 63a62c1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

1000-1100q/1002.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
3+
4+
You may return the answer in any order.
5+
6+
7+
8+
Example 1:
9+
10+
Input: ["bella","label","roller"]
11+
Output: ["e","l","l"]
12+
Example 2:
13+
14+
Input: ["cool","lock","cook"]
15+
Output: ["c","o"]
16+
'''
17+
18+
class Solution(object):
19+
def commonChars(self, A):
20+
"""
21+
:type A: List[str]
22+
:rtype: List[str]
23+
"""
24+
char_map = {}
25+
for char in A[0]:
26+
if char in char_map:
27+
char_map[char] += 1
28+
else:
29+
char_map[char] = 1
30+
31+
int_map = {}
32+
for index in range(1, len(A)):
33+
for char in char_map.keys():
34+
if char in A[index]:
35+
char_count = min(A[index].count(char), char_map[char])
36+
char_map[char] = char_count
37+
else:
38+
del char_map[char]
39+
40+
result = []
41+
for key, value in char_map.items():
42+
result.extend([key]*value)
43+
44+
return result

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
44

55
### LeetCode Algorithm
66

7+
##### [Problems 1000-1100](./1000-1100q/)
8+
| # | Title | Solution | Difficulty |
9+
|---| ----- | -------- | ---------- |
10+
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters)|[Python](./1000-1100q/1002.py)|Easy|
11+
712
##### [Problems 900-1000](./900-1000q/)
813
| # | Title | Solution | Difficulty |
914
|---| ----- | -------- | ---------- |

0 commit comments

Comments
 (0)