-
Notifications
You must be signed in to change notification settings - Fork 7
/
1178.cpp
39 lines (28 loc) · 908 Bytes
/
1178.cpp
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
class Solution {
public:
int maskWord(string word){
int mask = 0;
for(auto& w : word)
mask |= (1 << (w-'a'));
return mask;
}
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
unordered_map<int, int> freq;
for(auto& w : words)
freq[maskWord(w)]++;
vector<int> result;
for(auto& p : puzzles){
int mask = maskWord(p);
int submask = mask;
int first = 1 << (p[0]-'a');
int curr = 0;
while(submask){
if(submask & first)
curr += freq[submask];
submask = (submask - 1) & mask;
}
result.push_back(curr);
}
return result;
}
};