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
  • 相关阅读:
    细看运维85条军规,是否触动了你内心深处那根弦?
    SQL语句的CRUD
    sqlserver数据库类型对应Java中的数据类型
    Struts2文件上传--多文件上传(插件uploadify)
    web.xml文件详解
    关于Java的散列桶, 以及附上一个案例-重写map集合
    hibernate多表查询封装实体
    spring的7个模块
    Struts标签库详解【3】
    Struts标签库详解【2】
  • 原文地址:https://www.cnblogs.com/lyhcc/p/11443460.html
Copyright © 2011-2022 走看看