zoukankan      html  css  js  c++  java
  • 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"
    There're 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
  • 相关阅读:
    “零接触”新需求,如何快速实现体温检测数字化管控方案?
    AR公共安全及应急指挥中的应用 | TVP思享
    当模板方法遇到了委托函数,你的代码又可以精简了
    为什么要用内插字符串代替string.format
    如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析
    8天入门docker系列 —— 第八天 让程序跑在swarm集群上
    8天入门docker系列 —— 第七天 让你的container实现跨主机访问
    8天入门docker系列 —— 第六天 搭建自己的私有镜像仓库Registry
    8天入门docker系列 —— 第五天 使用aspnetcore小案例熟悉容器互联和docker-compose一键部署
    8天入门docker系列 —— 第四天 使用aspnetcore小案例熟悉端口映射和挂载目录
  • 原文地址:https://www.cnblogs.com/lyhcc/p/11443460.html
Copyright © 2011-2022 走看看