blob: d9e9ee7ceb6095b3482402d1c05178795fe04efb (
plain)
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
|
# 1002. Find Common Characters
from functools import reduce
from collections import Counter
"""
given a string array 'words' return an array of all characters that show up
in all strings within the 'words' including duplicates. you may return the
answer in any order.
"""
class Solution(object):
def commonChars(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
ans = Counter(words[0])
for w in words:
ans &= Counter(w)
return list(ans.elements())
# return list(reduce(Counter.__and__, map(Counter, words)).elements())
if __name__ == "__main__":
obj = Solution()
print(obj.commonChars(["bella", "label", "roller"]))
print(obj.commonChars(["cool", "lock", "cook"]))
|