문제: https://leetcode.com/problems/number-of-valid-words-for-each-puzzle/
Number of Valid Words for Each Puzzle - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
unordered_map<int, int> wordCount;
for(int i = 0; i < words.size(); i++){
int t = 0;
for(int j = 0; j < words[i].size(); j++){
t |= (1 << (words[i][j] - 'a'));
}
wordCount[t]++;
}
vector<int> ans;
for(int i = 0; i < puzzles.size(); i++){
int t1 = 0;
int firstChar = 1 << (puzzles[i][0] - 'a');
int cnt = wordCount[firstChar];
int mask = 0;
for(int j = 1; j < puzzles[i].size(); j++){
mask |= (1 << (puzzles[i][j] - 'a'));
}
for(int submask = mask; submask; submask = (submask - 1) & mask){
cnt += wordCount[submask | firstChar];
}
ans.push_back(cnt);
}
return ans;
}
};
-> 공부필요...
'* Computer Science > Algorithm' 카테고리의 다른 글
1286. Iterator for Combination (0) | 2021.11.14 |
---|---|
739. Daily Temperatures (0) | 2021.11.13 |
1368. Minimum Cost to Make at Least One Valid Path in a Grid (0) | 2021.11.04 |
1202. Smallest String With Swaps (0) | 2021.11.03 |
1711. Count Good Meals (0) | 2021.10.28 |