[LeetCode]#1002. Find Common Characters

Input: ["bella","label","roller"]
Output: ["e","l","l"]
Input: ["cool","lock","cook"]
Output: ["c","o"]
  1. Use Counter for counting A[0] string.
  2. Check c &= A[1] result
  3. Check c &= A[2] result
  4. Return result
from collections import Counter
class Solution:
def commonChars(self, A):
if not A:
return None

c = Counter(A[0])
for word in A[1:]:
c &= Counter(word)

ans = []
for x in c:
ans += [x] * c[x]
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.