1. 程式人生 > >LeetCode周賽#104 Q3 Word Subsets (map)

LeetCode周賽#104 Q3 Word Subsets (map)

問題描述

916. Word Subsets

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 aif every letter in b occurs in aincluding 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 Bb is a subset of a

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

Example 1:

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:

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

Example 4:

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

Example 5:

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

Note:

  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].

------------------------------------------------------------

題意

給定字串集合A和集合B,定義字串b是字串a的”subset”當且僅當b中字母出現次數小於等於a中對應字母出現的次數。對於A中的每個字串,判斷B中所有字串是不是都是它的”subset”.

------------------------------------------------------------

思路

這次周賽的題目好多都是用map做的。用map記錄B中各個字串中每個字元出現的次數,再用一個map求這些map的並。然後再用map記錄A中各個字串每個字元出現的次數,與前面求並的map作比較即可。

------------------------------------------------------------

程式碼

class Solution {
public:
    map<char, int> mp;
    
    void init(vector<string> &B)
    {
        int i, j, len, lB = B.size();
        map<char, int> bp;
        map<char, int>::iterator it;
        string b;
        for (i=0; i<lB; i++)
        {
            b = B.at(i);
            len = b.size();
            bp.clear();
            for (j=0; j<len; j++)
            {
                bp[b[j]]++;
            }
            for (it=bp.begin(); it!=bp.end(); it++)
            {
                if (mp[it->first] < it->second)
                {
                    mp[it->first] = it->second;
                }
            }
        }
    }
    
    vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
        init(B);
        int i, j, len, lA = A.size(), lB = B.size();
        string a;
        map<char, int> ap, bp;
        map<char, int>::iterator it;
        bool is_ans = true;
        vector<string> ans;
        for (i=0; i<lA; i++)
        {
            is_ans = true;
            a = A[i];
            len = a.size();
            ap.clear();
            for (j=0; j<len; j++)
            {
                ap[a[j]]++;
            }
            for (it=mp.begin(); it!=mp.end(); it++)
            {
                if (ap[it->first] < it->second)
                {
                    is_ans = false;
                    break;
                }
            }
            if (is_ans)
            {
                ans.push_back(a);
            }
        }
        return ans;
    }
};