-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
75 lines (56 loc) · 1.65 KB
/
solution.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Solution {
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
int n = words.size(), m = puzzles.size();
auto getmask = [&](string& s){
int ans = 0;
for(char c : s){
ans |= (1 << (c-'a'));
}
return ans;
};
unordered_map<int, int> mp;
for (auto& w: words) {
auto m = getmask(w);
mp[m]++;
}
vector<int> ret;
for (auto& p: puzzles){
auto curm = getmask(p);
int matches = 0;
for (auto it: mp){
int m = it.first;
int tmp = m | curm;
bool has1stl = (1 << (p[0]-'a')) & m;
if (tmp == curm && has1stl) matches += it.second;
}
ret.push_back(matches);
}
return ret;
/*const int N = 26;
int n = words.size(), m = puzzles.size();
auto getmask = [&](string& s){
int ans = 0;
for(char c : s){
ans |= (1 << (c-'a'));
}
return ans;
};
vector<int> cnt(1 << N,0);
for(string& s : words){
cnt[getmask(s)]++;
}
vector<int> fans(m,0);
for(int i = 0; i<m; i++){
int ans = 0;
int mask = getmask(puzzles[i]) ;
for(int s = mask; s > 0; s = (s-1) & mask){
int first = (1 << (puzzles[i][0] - 'a'));
if( (s & first) > 0)
ans += cnt[s];
}
fans[i] = ans;
}
return fans;*/
}
};