Leetcode 952 Solution

This article provides solution to leetcode question 952 (word-subsets)

https://leetcode.com/problems/word-subsets

Solution

class Solution:
    def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
        m = collections.defaultdict(int)

        for pattern in B:
            temp = collections.defaultdict(int)
            for ch in pattern:
                temp[ch] += 1
            for k, v in temp.items():
                m[k] = max(m[k], v)

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

            for k, v in m.items():
                if v > temp[k]:
                    break
            else:
                ans.append(word)

        return ans