-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln-1.cpp
31 lines (31 loc) · 864 Bytes
/
soln-1.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
class Solution {
public:
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
int chars[26] = {0};
for(const auto & word : B) {
int temp[26] = {0};
for(char ch : word) {
++temp[ch - 'a'];
}
for(int i = 0; i < 26; ++i) {
chars[i] = max(chars[i], temp[i]);
}
}
vector<string> ans;
for(const auto & word : A) {
int temp[26] = {0};
for(char ch : word) {
++temp[ch - 'a'];
}
bool valid = true;
for(int i = 0; i < 26; ++i) {
if (temp[i] < chars[i]) {
valid = false;
break;
}
}
if (valid) ans.push_back(word);
}
return ans;
}
};