0916.cpp (851B)
1 class Solution { 2 public: 3 vector<string> wordSubsets(const vector<string> &words1, const vector<string> &words2) const { 4 const int n = size(words1), m = size(words2); 5 int count[26] = {0}; 6 vector<string> res; 7 8 for (int i = 0; i < m; i++) { 9 int lcount[27] = {0}; 10 for (char c : words2[i]) 11 lcount[c - 'a']++; 12 for (int j = 0; j < 26; j++) { 13 count[j] = max(count[j], lcount[j]); 14 } 15 } 16 17 for (int i = 0; i < n; i++) { 18 int lcount[27] = {0}; 19 for (char c : words1[i]) 20 lcount[c - 'a']++; 21 for (int k = 0; k < 26; k++) { 22 if (lcount[k] < count[k]) goto next; 23 } 24 25 res.push_back(words1[i]); 26 next:; 27 } 28 29 return res; 30 } 31 };