LeetCode 916 Word Subsetse (Python)

Posted by 小明MaxMing on March 26, 2021

题目

We are given two arrays A and B of words. Each word is a string of lowercase letters.

Now, say that word b is a subset of word a if every letter in b occurs in a, including multiplicity. For example, “wrr” is a subset of “warrior”, but is not a subset of “world”.

Now say a word a from A is universal if for every b in B, b is a subset of a.

Return a list of all universal words in A. You can return the words in any order.

解题思路

将所有B中的所有单词,凑成一个单词,使这个字符串包含b中所有的单词,遍历A中所有单词,只要包含这个单词就可以

代码

class Solution:
    def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
        target = defaultdict(int)
        for word in B:
            for c, v in Counter(word).items():
                target[c] = max(target[c], v)
        res = []
        for word in A:
            ct = Counter(word)
            for c, v in target.items():
                if c not in ct or v > ct[c]:
                    break
            else:
                res.append(word)
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅