forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
34 lines (34 loc) · 891 Bytes
/
Solution.cpp
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
30
31
32
33
34
class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
int cnt[26] = {0};
int t[26];
for (auto& b : words2) {
memset(t, 0, sizeof t);
for (auto& c : b) {
t[c - 'a']++;
}
for (int i = 0; i < 26; ++i) {
cnt[i] = max(cnt[i], t[i]);
}
}
vector<string> ans;
for (auto& a : words1) {
memset(t, 0, sizeof t);
for (auto& c : a) {
t[c - 'a']++;
}
bool ok = true;
for (int i = 0; i < 26; ++i) {
if (cnt[i] > t[i]) {
ok = false;
break;
}
}
if (ok) {
ans.emplace_back(a);
}
}
return ans;
}
};