1178. Number of Valid Words for Each Puzzle

Topic Description

  This last question is the subject of September 1, 2019 LeetCode week race

Title Description

With respect to a given puzzle string, a word is valid if both the following conditions are satisfied:

  • word contains the first letter of puzzle.
  • For each letter in word, that letter is in puzzle

    For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage"; while invalid words are "beefed" (doesn't include "a") and "based" (includes "s" which isn't in the puzzle).

Return an array answer, where answer[i] is the number of words in the given word list words that are valid with respect to the puzzle puzzles[i].

Sample

Input: 
words = ["aaaa","asas","able","ability","actt","actor","access"], 
puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
Output: [1,1,3,2,4,0]
Explanation:
1 valid word for "aboveyz" : "aaaa" 
1 valid word for "abrodyz" : "aaaa"
3 valid words for "abslute" : "aaaa", "asas", "able"
2 valid words for "absoryz" : "aaaa", "asas"
4 valid words for "actresz" : "aaaa", "asas", "actt", "access"
There're no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.

Conditions

  • 1 <= words.length <= 10^5
  • 4 <= words[i].length <= 50
  • 1 <= puzzles.length <= 10^4
  • puzzles[i].length == 7
  • words[i][j]puzzles[i][j] are English lowercase letters.
  • Each puzzles[i] doesn't contain repeated characters.

Problem-solving ideas

① first traversal words again, and then the inside of the letters of each word as an index (i.e., inside the word letter -'a 'as an index), the word placed in the corresponding array

② calculating a unique value corresponding to a word

  I'm using 26 binary representation, for example, expressed as aba 11

  << 1 ((corresponding to the letter will not be repeated) - 'a') calculating the position of a character corresponding to

  The first 11 and the second representation b expressed as a, repeated letters are not considered

③ array traversal puzzles, also calculates a corresponding unique value corresponding to a word, and then using the first letter of a word lookup meet meaning of the title (title requirement is intended, the first letter of a word puzzle which is contained in the word), then traverse word corresponding to the array index

④ determining the subject word meets a condition

  The method used is the unique value is a unique value determined by the unique word Puzzle value corresponding to the operation of the bit operation and word operation is determined to be equal, the condition is satisfied

    如   word ="a"  , puzzle  = "ab"

    The only value of 111

    After the operation is a 1 &

  It should be noted that: a lower priority than the & operator ==

Code

    vector<int>w;
    vector<int>wf[26];
    bool vis[26];
    vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
        
        for(int i=0;i<words.size();i++){
            memset(vis,false,sizeof(vis));
            int tmp = 0;
            
            for(int j=0;j<words[i].size();j++){
                if(!vis[words[i][j]-'a']){
                    tmp+=(1<<(words[i][j]-'a'));
                    vis[words[i][j]-'a']=true;
                    wf[words[i][j]-'a'].push_back(i);
                    
                }
            }

            w.push_back(tmp);
        }
        
        vector<int>res;
        for(int i=0;i<puzzles.size();i++){
            int index = puzzles[i][0]-'a';
            int h  = 0;
            for(int j=0;j<7;j++){
                h+=(1<<(puzzles[i][j]-'a'));
            }
            int cnt =0 ;
            for(int j=0;j<wf[index].size();j++){        
                if((w[wf[index][j]]&h)==w[wf[index][j]]){
                    cnt++;
                }
            }
            res.push_back(cnt);
        }
        return res;
        
    }
Code

Guess you like

Origin www.cnblogs.com/lyhcc/p/11443460.html