[LeetCode] 916. Word Subsets 單詞子集合



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

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".git

Now say a word a from A is universal if for every b in Bb is a subset of agithub

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

Example 1:app

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
Output: ["facebook","google","leetcode"]

Example 2:函數

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
Output: ["apple","google","leetcode"]

Example 3:google

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
Output: ["facebook","google"]

Example 4:code

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
Output: ["google","leetcode"]

Example 5:htm

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

Note:blog

  1. 1 <= A.length, B.length <= 10000
  2. 1 <= A[i].length, B[i].length <= 10
  3. A[i] and B[i] consist only of lowercase letters.
  4. All words in A[i] are unique: there isn't i != j with A[i] == A[j].



這道題定義了兩個單詞之間的一種子集合關係,就是說假如單詞b中的每一個字母都在單詞a中出現了(包括重複字母),就說單詞b是單詞a的子集合。如今給了兩個單詞集合A和B,讓找出集合A中的全部知足要求的單詞,使得集合B中的全部單詞都是其子集合。配合上題目中給的一堆例子,意思並不難理解,根據子集合的定義關係,其實就是說若單詞a中的每一個字母的出現次數都大於等於單詞b中每一個字母的出現次數,單詞b就必定是a的子集合。如今因爲集合B中的全部單詞都必須是A中某個單詞的子集合,那麼其實只要對於每一個字母,都統計出集合B中某個單詞中出現的最大次數,好比對於這個例子,B=["eo","oo"],其中e最多出現1次,而o最多出現2次,那麼只要集合A中有單詞的e出現很多1次,o出現很多於2次,則集合B中的全部單詞必定都是其子集合。這就是本題的解題思路,這裏使用一個大小爲 26 的一維數組 charCnt 來統計集合B中每一個字母的最大出現次數,而將統計每一個單詞的字母次數的操做放到一個子函數 helper 中,當 charCnt 數組更新完畢後,下面就開始檢驗集合A中的全部單詞了。對於每一個遍歷到的單詞,仍是要先統計其每一個字母的出現次數,而後跟 charCnt 中每一個位置上的數字比較,只要均大於等於 charCnt 中的數字,就能夠加入到結果 res 中了,參見代碼以下:


class Solution {
public:
    vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
        vector<string> res;
        vector<int> charCnt(26);
        for (string &b : B) {
            vector<int> t = helper(b);
            for (int i = 0; i < 26; ++i) {
                charCnt[i] = max(charCnt[i], t[i]);
            }
        }
        for (string &a : A) {
            vector<int> t = helper(a);
            int i = 0;
            for (; i < 26; ++i) {
                if (t[i] < charCnt[i]) break;
            }
            if (i == 26) res.push_back(a);
        }
        return res;
    }
    vector<int> helper(string& word) {
        vector<int> res(26);
        for (char c : word) ++res[c - 'a'];
        return res;
    }
};



Github 同步地址:

https://github.com/grandyang/leetcode/issues/916



參考資料:

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

https://leetcode.com/problems/word-subsets/discuss/175854/C%2B%2BJavaPython-Straight-Forward



LeetCode All in One 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索