[
hash table
counter
string
]
Leetcode 1002. Find Common Characters
Problem statement
https://leetcode.com/problems/find-common-characters/
Solution
For every word create counter, then intersect all counters.
Complexity
It is O(n)
for time, where n
is total length of all words and O(max(26, m))
for space, where m
is the length of answer.
Code
class Solution:
def commonChars(self, A):
lst = reduce(lambda x, y: x & y, list(map(lambda x: Counter(x), A)))
ans = []
for x, y in lst.items():
ans += [x] * y
return ans