---
# System prepended metadata

title: 0916. Word Subsets
tags: [Leetcode, Medium]

---

# 0916. Word Subsets
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/word-subsets/
## 思路
非常直觉的一道题
先算出words2里面所有字母出现的最大freq ```cnt```
然后看words1里面的每个单词的字母的freq count 看有没有所有字母的count都比```cnt```大的
## Code
```java=
class Solution {
    public List<String> wordSubsets(String[] words1, String[] words2) {
        int[] cnt = new int[26];
        for(String word:words2){
            int[] tempCnt = count(word);
            for(int i=0; i<cnt.length; i++){
                cnt[i] = Math.max(cnt[i], tempCnt[i]);
            }
        }
        
        List<String> ans = new ArrayList<>();
        for(String word:words1){
            int[] tempCnt = count(word);
            int i;
            for(i=0; i<tempCnt.length; i++){
                if(cnt[i]>tempCnt[i]) break;
            }
            if(i==tempCnt.length) ans.add(word);
        }
        return ans;
    }
    private int[] count(String word){
        int[] cnt = new int[26];
        for(int i=0; i<word.length(); i++){
            cnt[word.charAt(i)-'a']++;
        }
        return cnt;
    }
}
```