contains category wise problems(data structures, competitive) of popular platforms.
View the Project on GitHub mayankdutta/category-wise-problems
1178. Number of Valid Words for Each Puzzle, Hard
Resources
Approach
class Solution {
public:
int encrypt(string word) {
int mask = 0;
for (const auto &w : word) {
mask |= (1 << (w - 'a'));
}
return mask;
}
vector<int> findNumOfValidWords(vector<string> &words,
vector<string> &puzzles) {
map<int, int> mapWord;
for (const auto &word : words) {
mapWord[encrypt(word)]++;
}
vector<int> ans;
for (const auto &p : puzzles) {
int mask = encrypt(p);
int first = (1 << (p.front() - 'a'));
int count = 0;
for (int submask = mask; submask; submask = (submask - 1) & mask) {
if (submask & first) {
count += mapWord[submask];
}
}
ans.push_back(count);
}
return ans;
}
};