-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-common-characters.py
46 lines (36 loc) · 1.07 KB
/
find-common-characters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# https://leetcode.com/problems/find-common-characters/
# Example 1:
# Input: ["bella","label","roller"]
# Output: ["e","l","l"]
# Example 2:
# Input: ["cool","lock","cook"]
# Output: ["c","o"]
# Note:
# 1 <= A.length <= 100
# 1 <= A[i].length <= 100
# A[i][j] is a lowercase letter
class Solution:
def commonChars(self, A: list) -> list:
util_dict = {}
res = []
for ch in A[0]:
if ch in util_dict:
util_dict[ch] += 1
else : util_dict[ch] = 1
for ch in util_dict:
for item in A[1:]:
if item.count(ch) == util_dict[ch]:
continue
else:
util_dict[ch] = min(util_dict[ch] , item.count(ch) )
res = []
for ch in A[0]:
if util_dict[ch] == 0:
continue
if ch in util_dict:
res.append(ch)
util_dict[ch] -= 1
return res
sol = Solution()
print(sol.commonChars(["bella","label","roller"]))
print(sol.commonChars(["cool","lock","cook"]))