9 {
10 vector<int> total_occurrences(26, 0);
11 vector<string> answer;
12 answer.reserve((int)words1.size());
13 for (string& word: words2)
14 {
15 vector<int> occurrences(26, 0);
16 for (char c: word)
17 occurrences[c - 'a']++;
18 for (int i = 0; i < 26; ++i)
19 total_occurrences[i] = max(occurrences[i], total_occurrences[i]);
20 }
21 for (string& word: words1)
22 {
23 vector<int> occurrences(26, 0);
24 for (char c: word)
25 occurrences[c - 'a']++;
26 bool accept = true;
27 for (int i = 0; i < 26; ++i)
28 {
29 if (occurrences[i] < total_occurrences[i])
30 {
31 accept = false;
32 break;
33 }
34 }
35 if (accept)
36 answer.push_back(word);
37 }
38 return answer;
39 }