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))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章