LeetCode #916 Word Subsets 单词子集 916 Word Subsets 单词子集

916 Word Subsets 单词子集

Description:
You are given two string arrays words1 and words2.

A string b is a subset of string 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".
A string a from words1 is universal if for every string b in words2, b is a subset of a.

Return an array of all the universal strings in words1. You may return the answer in any order.

Example:

Example 1:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]
Example 2:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]

Example 3:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","oo"]
Output: ["facebook","google"]

Example 4:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["lo","eo"]
Output: ["google","leetcode"]

Example 5:

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["ec","oc","ceo"]
Output: ["facebook","leetcode"]

Constraints:

1 <= words1.length, words2.length <= 10^4
1 <= words1[i].length, words2[i].length <= 10
words1[i] and words2[i] consist only of lowercase English letters.
All the strings of words1 are unique.

题目描述:
我们给出两个单词数组 A 和 B。每个单词都是一串小写字母。

现在,如果 b 中的每个字母都出现在 a 中,包括重复出现的字母,那么称单词 b 是单词 a 的子集。 例如,“wrr” 是 “warrior” 的子集,但不是 “world” 的子集。

如果对 B 中的每一个单词 b,b 都是 a 的子集,那么我们称 A 中的单词 a 是通用的。

你可以按任意顺序以列表形式返回 A 中所有的通用单词。

示例 :

示例 1:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
输出:["facebook","google","leetcode"]

示例 2:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
输出:["apple","google","leetcode"]

示例 3:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
输出:["facebook","google"]

示例 4:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
输出:["google","leetcode"]

示例 5:

输入:A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
输出:["facebook","leetcode"]

提示:

1 <= A.length, B.length <= 10000
1 <= A[i].length, B[i].length <= 10
A[i] 和 B[i] 只由小写字母组成。
A[i] 中所有的单词都是独一无二的,也就是说不存在 i != j 使得 A[i] == A[j]。

思路:

计数
先记录 B 中出现的所有字符串的所有字符的最大出现频率
然后遍历 A 中所有字符串, 如果 A 中的字符串中字符频率不小于 B 的最大出现频率则加入 result
时间复杂度为 O(m + n), 空间复杂度为 O(m + n), m, n 分别为 A, B 数组的长度

代码:
C++:

class Solution
{
public:
    vector<string> wordSubsets(vector<string>& words1, vector<string>& words2)
    {
        vector<int> count(26, 0);
        for (const auto& word : words2)
        {
            vector<int> cur(26, 0);
            for (const auto& c : word) ++cur[c - 'a'];
            for (int i = 0; i < 26; i++) count[i] = max(count[i], cur[i]);
        }
        vector<string> result;
        for (const auto& word : words1)
        {
            vector<int> cur(26, 0);
            for (const auto& c : word) ++cur[c - 'a'];
            bool is_found = true;
            for (int i = 0; i < 26; i++)
            {
                if (count[i] > cur[i])
                {
                    is_found = false;
                    break;
                }
            }
            if (is_found) result.emplace_back(word);
        }
        return result;
    }
};

Java:

class Solution {
    public List<String> wordSubsets(String[] words1, String[] words2) {
        int count[] = new int[26];
        for (String word : words2) {
            int cur[] = new int[26];
            for (char c : word.toCharArray()) ++cur[c - 'a'];
            for (int i = 0; i < 26; i++) count[i] = Math.max(count[i], cur[i]);
        }
        List<String> result = new ArrayList<>();
        for (String word : words1) {
            int cur[] = new int[26];
            for (char c : word.toCharArray()) ++cur[c - 'a'];
            boolean isFound = true;
            for (int i = 0; i < 26; i++)
            {
                if (count[i] > cur[i])
                {
                    isFound = false;
                    break;
                }
            }
            if (isFound) result.add(word);
        }
        return result;
    }
}

Python:

class Solution:
    def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
        c2 = reduce(collections.Counter.__or__, map(Counter, words2))
        return list(filter(lambda word: all(v <= Counter(word)[k] for k, v in c2.items()), words1))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章