We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents e7791ad + f711793 commit 59e38a3Copy full SHA for 59e38a3
916. Word Subsets
@@ -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