-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path916. Word Subsets.cpp
45 lines (34 loc) · 1.05 KB
/
916. Word Subsets.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
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
vector<string>res;
vector<int>maxCount(26);
for (int i = 0; i < B.size(); ++i) {
vector<int> v = getCount(B[i]);
for (int j = 0; j < 26; ++j) {
maxCount[j] = max(maxCount[j], v[j]);
}
}
for (int i = 0; i < A.size(); ++i) {
vector<int> v = getCount(A[i]);
bool isValid(true);
for (int j = 0; j < 26; ++j) {
if (v[j] < maxCount[j]) {
isValid = false;
break;
}
}
if (isValid) {
res.push_back(A[i]);
}
}
return res;
}
vector<int> getCount(string& s) {
vector<int>cnt(26);
for (int i = 0; i < s.size(); ++i) {
cnt[s[i] - 'a']++;
}
return cnt;
}
};