1178. Number of Valid Words for Each Puzzle

Posted lyhcc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1178. Number of Valid Words for Each Puzzle相关的知识,希望对你有一定的参考价值。

题目说明

  本题目是2019年9月1日LeetCode周赛的最后一题

题目描述

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].

样例

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"
Therere no valid words for "gaswxyz" cause none of the words in the list contains letter g.

条件限制

  • 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.

解题思路

① 先将words遍历一遍,然后将里面的每个单词的字母作为一个索引(即单词里面的字母-‘a’作为索引),将单词放到对应数组中

② 计算单词对应的一个唯一的数值

  我使用26个二进制表示,例如aba表示为11

  1<<((对应不重复字母)-‘a’)计算一个字符对应的位置

  而11 的第一个表示b 第二个表示为a ,重复字母不考虑

③ 遍历puzzles数组,也计算相应的单词对应唯一的值,然后利用第一个字母查找符合题意的word(题意要求是,将puzzle里面的单词的首个字母包含于word中),然后遍历对应索引的word数组

④ 判断word是否满足题目条件

  而使用的方法是位运算中的与运算通过判断 puzzle对应的唯一值与word的唯一值进行与运算判断是否等于word的唯一值,满足则是满足条件

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

    唯一值  1      11

    &运算过后 为  1

  这里需要注意的是:&运算的优先级低于==

代码

技术图片
    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

以上是关于1178. Number of Valid Words for Each Puzzle的主要内容,如果未能解决你的问题,请参考以下文章

65. Valid Number

611. Valid Triangle Number

65. Valid Number *HARD*

Leetcode: Valid Word Abbreviation

611. Valid Triangle Number

611. Valid Triangle Number