-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberofValidWordsforEachPuzzle.cpp
More file actions
38 lines (36 loc) · 1.07 KB
/
numberofValidWordsforEachPuzzle.cpp
File metadata and controls
38 lines (36 loc) · 1.07 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
// Source: https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/
// Author: Miao Zhang
// Date: 2021-04-15
class Solution {
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
vector<int> res;
unordered_map<int, int> freq;
for (const string& word: words) {
int mask = 0;
for (char c: word) {
mask |= 1 << (c - 'a');
}
freq[mask]++;
}
for (const string& p: puzzles) {
int mask = 0;
for (char c: p) {
mask |= 1 << (c - 'a');
}
int firstcapital = p[0] - 'a';
int cur = mask;
int total = 0;
while (cur) {
if ((cur >> firstcapital) & 1) {
auto it = freq.find(cur);
if (it != freq.end())
total += it->second;
}
cur = (cur - 1) & mask;
}
res.push_back(total);
}
return res;
}
};