Skip to content

Commit 59e38a3

Browse files
authored
Create 916. Word Subsets (#686)
2 parents e7791ad + f711793 commit 59e38a3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

916. Word Subsets

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
array<int, 26>frequency(string w){
4+
array<int, 26>freq={0};
5+
for(const char& c:w)
6+
freq[c-'a']++;
7+
return freq;
8+
}
9+
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
10+
vector<string> res;
11+
array<int,26>max_freq={0};
12+
for(const string& b:words2){
13+
array<int,26>freq=frequency(b);
14+
for(int i=0;i<26;i++)
15+
max_freq[i]=max(max_freq[i],freq[i]);
16+
}
17+
for(const string& a:words1){
18+
array<int,26>freq=frequency(a);
19+
bool uni=true;
20+
for(int i=0;i<26;i++){
21+
if(freq[i]<max_freq[i]){
22+
uni=false;
23+
break;
24+
}
25+
}
26+
if(uni)
27+
res.push_back(a);
28+
}
29+
return res;
30+
}
31+
};

0 commit comments

Comments
 (0)