diff options
Diffstat (limited to 'find_common_char.py')
-rw-r--r-- | find_common_char.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/find_common_char.py b/find_common_char.py new file mode 100644 index 0000000..d9e9ee7 --- /dev/null +++ b/find_common_char.py @@ -0,0 +1,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"])) |