-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWord Subset.cpp
More file actions
48 lines (43 loc) · 1.17 KB
/
Word Subset.cpp
File metadata and controls
48 lines (43 loc) · 1.17 KB
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
46
47
48
/*
Platform :- Leetcode
Problem :- Word Subset
Hint :- Pre compute the frequency per character of B ( total characters are 26)
*/
class Solution {
public:
vector<string> wordSubsets(vector<string>& A, vector<string>& B) {
vector<string>ans;
map<string,vector<int>>P,Q;
for(int i=0;i<A.size();++i){
vector<int>Z(26);
for(int k=0;k<A[i].size();++k){
Z[A[i][k]-'a']++;
//cout<<(A[i][k]-'a')<<" ";
}
//cout<<endl;
P[A[i]]=Z;
}
vector<int>fi(26);
for(int i=0;i<B.size();++i){
vector<int>Z(26);
for(int k=0;k<B[i].size();++k){
Z[B[i][k]-'a']++;
}
for(int j=0;j<26;++j){
fi[j]=max(fi[j],Z[j]);
}
}
vector<int>z;
for(int i=0;i<A.size();++i){
int f=0;
z=P[A[i]];
for(int j=0;j<26;++j){
if(z[j]<fi[j]){
f=1;break;
}
}
if(f==0)ans.push_back(A[i]);
}
return ans;
}
};