Leetcode 1044 Solution

This article provides solution to leetcode question 1044 (find-common-characters)

https://leetcode.com/problems/find-common-characters

Solution

class Solution(object):
    def commonChars(self, A):
        """
        :type A: List[str]
        :rtype: List[str]
        """
        c = collections.defaultdict(int)

        for word in A:
            m = collections.defaultdict(int)
            for ch in word:
                m[ch] += 1

            if not c:
                c = m.copy()
            else:
                for k, v in c.items():
                    c[k] = min(v, m[k])

        ans = []
        for k, v in c.items():
            for _ in range(v):
                ans.append(k)

        return ans