LeetCode -- 916. 单词子集
本题,我们可以先统计出words2中所有单词中每个字母出现的最大频次;再和words1中的每个单词依次进行比较。
python class Solution: def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]: cnt = Counter() for it in words2: ct = Counter(it) for it2 in ct: cnt[it2] = max(cnt[it2], ct[it2]) res = [] for wd in words1: ct = Counter(wd) flag = True for it2 in cnt: if cnt[it2] > ct[it2]: flag = False break if flag: res.append(wd) return res
c ++ class Solution { public: vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) { vector<vector<int>> f(words1.size(), vector<int>(26)); for(int i = 0; i < words1.size(); i ++ ) { for(auto c : words1[i]) { f[i][c - 'a'] ++ ; } } int cnt[26] = {0}, tmp[26] = {0}; for(int i = 0; i < words2.size(); i ++ ) { memset(tmp, 0, sizeof(tmp)); for(auto c : words2[i]) { tmp[c - 'a'] ++ ; } for(int i = 0; i < 26; i ++ ) { cnt[i] = max(cnt[i], tmp[i]); } } vector<string> res; for(int i = 0; i < words1.size(); i ++ ) { bool flag = true; for(int j = 0; j < 26; j ++ ) { if(f[i][j] < cnt[j]) { flag = false; break; } } if(flag) res.push_back(words1[i]); } return res; } };
java class Solution { public int[] count(String s) { int[] res = new int[26]; for(int i = 0; i < s.length(); i ++ ) { res[s.charAt(i) - 'a'] ++ ; } return res; } public List<String> wordSubsets(String[] words1, String[] words2) { int[] cnt = count(""); for(String it : words2){ int[] tt = count(it); for(int i = 0; i < 26; i ++ ) { cnt[i] = Math.max(cnt[i], tt[i]); } } List<String> res = new ArrayList(); for(String it : words1) { int[] tt = count(it); boolean flag = true; for(int i = 0; i < 26; i ++ ) { if(cnt[i] > tt[i]) { flag = false; break; } } if(flag) { res.add(it); } } return res; } }
golang func max(a int, b int) int { if a > b { return a } return b } func wordSubsets(words1 []string, words2 []string) []string { cnt := make([]int, 26) for _, val := range words2 { tt := make([]int, 26) for i := range val { tt[val[i] - 'a'] ++ ; } for i := range cnt { cnt[i] = max(cnt[i], tt[i]) } } var res []string for _, val := range words1 { tt := make([]int, 26) for i := range val { tt[val[i] - 'a'] ++ } flag := true for it := range cnt { if cnt[it] > tt[it] { flag = false break } } if flag { res = append(res, val) } } return res }
js /** * @param {string[]} words1 * @param {string[]} words2 * @return {string[]} */ var wordSubsets = function(words1, words2) { let cnt = new Array(26).fill(0); let res = []; for(let i = 0; i < words2.length; i ++ ) { let tmp = new Array(26).fill(0); for(let j = 0; j < words2[i].length; j ++ ) { tmp[words2[i][j].charCodeAt() - 'a'.charCodeAt()] ++ ; } for(let j = 0; j < 26; j ++ ) { cnt[j] = Math.max(cnt[j], tmp[j]); } } for(let i = 0; i < words1.length; i ++ ) { let tmp = new Array(26).fill(0); for(let j = 0; j < words1[i].length; j ++ ) { tmp[words1[i][j].charCodeAt() - 'a'.charCodeAt()] ++ ; } let flag = true; for(let j = 0; j < 26; j ++ ) { if(cnt[j] > tmp[j]) { flag = false; break; } } if(flag) { res.push(words1[i]); } } return res; };