916. Word Subsets(python+cpp)

题目:

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.
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 <= A.length, B.length <= 10000
1 <= A[i].length, B[i].length <= 10
A[i] and B[i] consist only of lowercase letters.
All words in A[i] are unique: there isn’t i != j with A[i] == A[j].

解释:
需要统计B中每个单词中的每个字母在各自的单词中所出现的最大的次数,构成一个属于B的字典,只需拿这个字典和A中的每个a比较即可。
python用defaultdict速度比用Counter()速度快。
python代码:

from collections import Counter
class Solution:
    def wordSubsets(self, A, B):
        """
        :type A: List[str]
        :type B: List[str]
        :rtype: List[str]
        """
        result=[]
        count_B={}
        
        for b in B:
            count=Counter(b)
            for key in count:
                count_B[key]=max(count_B.get(key,0),count[key])
        print (count_B)
        for a in A:
            count_a=Counter(a)
            flag=True
            for key in count_B:
                if count_a.get(key,0)<count_B[key]:
                    flag=False
                    break
            if flag:
                result.append(a)
        return result
            

python代码(使用defaultdict速度更快):

from collections import defaultdict
class Solution:
    def wordSubsets(self, A, B):
        """
        :type A: List[str]
        :type B: List[str]
        :rtype: List[str]
        """
        result=[]
        count_B=defaultdict(int)
        
        for b in B:
            count=defaultdict(int)
            for letter in b:
                count[letter]+=1
            for key in count:
                count_B[key]=max(count_B[key],count[key])
        for a in A:
            count_a=defaultdict(int)
            for letter in a:
                count_a[letter]+=1
            flag=True
            for key in count_B:
                if count_a[key]<count_B[key]:
                    flag=False
                    break
            if flag:
                result.append(a)
        return result

c++代码:

#include <map>
using namespace std;
class Solution {
public:
    vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
        map<int,int>count_B;
        for (auto b:B)
        {
            map<int,int>count_b;
            for(auto letter:b)
            {
                count_b[letter]+=1;
            }
            for (auto item:count_b)
                count_B[item.first]=max(item.second,count_B[item.first]);
        
        }
        vector<string> result;
        for (auto a:A)
        {
            map<int,int>count_a;
            for(auto letter:a)
                count_a[letter]+=1;
            bool flag=true;
            for (auto item:count_B)
            {
                if (count_a[item.first]<item.second)
                {
                    flag=false;
                    break;
                }
            }
            if (flag)
               result.push_back(a); 
        }
        return result;
    }
};

总结:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值