-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordSubsets.cpp
More file actions
29 lines (28 loc) · 810 Bytes
/
wordSubsets.cpp
File metadata and controls
29 lines (28 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
vector<int> code(26, 0);
for (auto &s: words2) {
vector<int> count(26, 0);
for (char c: s)
++count[c - 'a'];
for (int i = 0; i < 26; ++i)
if (code[i] < count[i])
code[i] = count[i];
}
vector<string> ans;
for (auto &s: words1) {
vector<int> count(26, 0);
for (char c: s) {
++count[c - 'a'];
}
int i = 0;
for (; i < 26; ++i)
if (code[i] > count[i])
break;
if (i == 26)
ans.push_back(s);
}
return ans;
}
};