Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int shift[27];
- int Bit(string &str){
- int len = str.size();
- int alph[27];
- for(int i=1;i<=26;i++)
- alph[i] = false;
- for(int i=0;i<len;i++){
- alph[ str[i] - 'a' + 1 ] = true;
- }
- int bit = 0;
- for(int i=1;i<=26;i++){
- if(alph[i])
- bit = bit | shift[i];
- }
- return bit;
- }
- vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
- shift[0] = 1;
- for(int i=1;i<=26;i++)
- shift[i] = shift[i-1] * 2;
- int nw = words.size(), np = puzzles.size();
- int bitp[np + 1];
- unordered_map <int, int> bitw;
- for(int i=0;i<nw;i++){
- bitw[ Bit(words[i]) ] ++;
- }
- for(int i=0;i<np;i++){
- bitp[i] = Bit(puzzles[i]);
- }
- vector <int> ans;
- for(int i=0;i<np;i++){
- int cnt = 0;
- int fln = puzzles[i][0] - 'a' + 1;
- for(auto j: bitw){
- int bw = j.first;
- int c = j.second;
- if((bw & shift[fln]) == shift[fln] and (bitp[i] & bw) == bw)
- cnt += c;
- }
- ans.push_back(cnt);
- }
- return ans;
- }
- };
Add Comment
Please, Sign In to add comment