# LeetCode 916. Word Subsets https://leetcode.com/problems/word-subsets/description/ ## 題目大意 如題 ## 思考 用 hashtable 的觀念即可 C++ 參考解答: ```cpp! class Solution { public: vector<string> wordSubsets(vector<string> &words1, vector<string> &words2) { int cnt[26] = {0}; for (const auto &b : words2) { int temp[26] = {0}; for (char c : b) { ++temp[c - 'a']; } for (int i = 0; i < 26; ++i) { cnt[i] = max(cnt[i], temp[i]); } } vector<string> ans; ans.reserve(words1.size()); for (const auto &a : words1) { int temp[26] = {0}; for (char c : a) { ++temp[c - 'a']; } bool isUniversal = true; for (int i = 0; i < 26; ++i) { if (temp[i] < cnt[i]) { isUniversal = false; break; } } if (isUniversal) ans.push_back(a); } return ans; } }; ``` Go 參考解答: ```go! func wordSubsets(words1 []string, words2 []string) []string { cnt := make([]int, 26) for _, b := range words2 { temp := make([]int, 26) for _, c := range b { temp[c-'a']++ } for i := 0; i < 26; i++ { if temp[i] > cnt[i] { cnt[i] = temp[i] } } } ans := make([]string, 0, len(words1)) for _, a := range words1 { temp := make([]int, 26) for _, c := range a { temp[c-'a']++ } isUniversal := true for i := 0; i < 26; i++ { if temp[i] < cnt[i] { isUniversal = false break } } if isUniversal { ans = append(ans, a) } } return ans } ```