-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordSubsets.cpp
More file actions
39 lines (36 loc) · 981 Bytes
/
wordSubsets.cpp
File metadata and controls
39 lines (36 loc) · 981 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
30
31
32
33
34
35
36
37
38
39
// Source: https://leetcode.com/problems/word-subsets/
// Author: Miao Zhang
// Date: 2021-03-24
class Solution {
public:
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
vector<int> comb(26);
for (const string& b: B) {
vector<int> cur(count(b));
for (int i = 0; i < 26; i++) {
comb[i] = max(comb[i], cur[i]);
}
}
vector<string> res;
for (const string& a: A) {
vector<int> cur(count(a));
bool flag = true;
for (int i = 0; i < 26; i++) {
if (cur[i] < comb[i]) {
flag = false;
break;
}
}
if (flag) res.push_back(a);
}
return res;
}
private:
vector<int> count(const string& a) {
vector<int> cnt(26);
for (char c: a) {
cnt[c - 'a']++;
}
return cnt;
}
};