-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1178. Number of Valid Words for Each Puzzle.cpp
93 lines (93 loc) · 2.9 KB
/
1178. Number of Valid Words for Each Puzzle.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Solution {
public:
// optimized
vector<int> findNumOfValidWords1(vector<string>& words, vector<string>& puzzles) {
int sw = words.size();
int sp = puzzles.size();
vector<int> iw(sw,0);
vector<int> ip(sp,0);
vector<vector<int>> cbuckets(26);
for(int i=0;i!=sw;++i)
{
for(const auto& c:words[i])
{
int pos = c -'a';
iw[i] |= 1<<pos;
}
}
for(int i=0;i!=sw;++i)
{
for(int j = 0; j!=26 ; ++j)
{
if(iw[i] & 1<<j)
cbuckets[j].push_back(iw[i]);
}
}
for(int i=0;i!=sp;++i)
{
for(const auto& c:puzzles[i])
{
ip[i] |= 1<<(c-'a');
}
}
vector<int> res(sp,0);
// now search puzzles for every matching word
for(int i=0;i!=sp;++i)
{
int count = 0;
int firstChar = puzzles[i][0]-'a';
for(int w:cbuckets[firstChar])
{
int x = w ^ ip[i];
int y = x | ip[i];
if(y != ip[i])
continue;
++count;
}
res[i] = count;
}
return res;
}
// from editorial
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
int sw = words.size();
int sp = puzzles.size();
unordered_map<int,int> wordCount;
function<int(const string&)> bitmask = [](const string& str)
{
int mask(0);
for(const auto& c:str)
mask |= 1<<(c -'a');
return mask;
};
for(int i=0;i!=sw;++i)
wordCount[bitmask(words[i])]++;
vector<int> res;
for(auto& puzzle: puzzles)
{
int first = 1<<(puzzle[0] - 'a');
int count = wordCount[first];
// Make bitmask but ignore the first character since it must always be there.
int mask = bitmask(puzzle.substr(1));
// iterate over the submask
for (int submask = mask; submask; submask = (submask - 1) & mask) {
count += wordCount[submask | first]; // add first character
}
res.push_back(count);
}
return res;
}
};