Skip to content

Commit dccc9eb

Browse files
committed
Add C++ array solution of problem #916
1 parent 97b8919 commit dccc9eb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

916/solution_array.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<string> wordSubsets(vector<string>& w1, vector<string>& w2) {
4+
vector<int> p(26); // count # needed maximum characters
5+
for(auto w : w2) {
6+
vector<int> tmp(26);
7+
for(char c : w)
8+
tmp[c - 'a']++;
9+
for(int i = 0; i < 26; i++)
10+
p[i] = max(p[i], tmp[i]);
11+
}
12+
13+
vector<string> ans;
14+
for(auto w : w1) {
15+
vector<int> tmp(26);
16+
for(char c : w)
17+
tmp[c - 'a']++;
18+
19+
bool flag = true;
20+
for(int i = 0; i < 26; i++) {
21+
if(tmp[i] < p[i])
22+
flag = false;
23+
}
24+
if(flag)
25+
ans.push_back(w);
26+
}
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)