题目如下:

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

Example :

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

Constraints:

  • 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中的每个单词都做去掉重复字符处理,然后按字典序重新把字符排序,得到新的单词,例如 ealebb 经过处理后就是 able。接下来对puzzles中的每个单词除第一个字符外的子字符串做同样的处理,例如 gawbxyz 处理后的结果是 g + abwxyz 两部分。对于满足gawbxyz谜底的单词要具备的条件是 第一个字符g是必定存在,同时后面的abwxyz 任意选择0~6个字符,这要判断这些组合有哪些在处理words中出现过,即可得puzzles中每个puzzle的结果。因为puzzles[i].length == 7,所以最多只会有2^6次方中组合,不会存在性能问题。

代码如下:

class Solution(object):
def findNumOfValidWords(self, words, puzzles):
"""
:type words: List[str]
:type puzzles: List[str]
:rtype: List[int]
"""
dic_words = {}
for word in words:
newword = ''.join(sorted(list(set(list(word)))))
dic_words[newword] = dic_words.setdefault(newword,0) + 1
res = []
for puzzle in puzzles:
count = 0
first = puzzle[0]
puzzle = ''.join(sorted(list(set(list(puzzle[1:])))))
if puzzle.find(first) != -1:
inx = puzzle.find(first)
puzzle = puzzle[:inx] + puzzle[inx+1:]
if first in dic_words:
count += dic_words[first]
queue = []
for (i,v) in enumerate(puzzle):
queue.append((v,i))
while len(queue) > 0:
v,i = queue.pop(0)
if i == len(puzzle) - 1:
key = ''.join(sorted(list(first + v)))
if key in dic_words:
count += dic_words[key]
continue
queue.append((v,i+1))
queue.append((v + puzzle[i+1],i+1))
res.append(count)
return res

【leetcode】1178. Number of Valid Words for Each Puzzle的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  5. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  6. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  7. 【leetcode】996. Number of Squareful Arrays

    题目如下: Given an array A of non-negative integers, the array is squareful if for every pair of adjacen ...

  8. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  9. 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...

随机推荐

  1. 用poi来导出数据到excel文档

    package cn.com.dyg.work.common.utils; import org.apache.poi.hssf.usermodel.HSSFRichTextString; impor ...

  2. Django的Mov逻辑的管理特色

    Django的MOV逻辑的管理特色 首先我们谈论到一个逻辑上的概念都从它的起点说起,在我看来mov的起点肯定就是Model了,那么Model有什莫特色呢 如果一个项目定义的Django那么Django ...

  3. Java基础/网络经验

    一.Java新特性好文--掘金 1.Java8 新特性指导手册 2.Java 11 已发布,String 还能这样玩 二.Java避坑 1.为什么阿里巴巴不建议在for循环中使用"+&quo ...

  4. AspNet Core Swagger4.0 生成请求model描述

    今天给大家分享 swagger 俩个冷门的小技巧 获取控制器描述 将 IncludeXmlComments 方法第二个参数设置为 true 即可 public static void IncludeX ...

  5. MAS多媒体的整个存储架构是怎样的?

    MAS多媒体的整个存储架构是怎样的?

  6. [Python3] 032 常用模块 random

    目录 random 1. random.random() 2. random.choice() 3. random.shuffle() 4. random.randint() 5. random.ra ...

  7. multiplication_puzzle(区间dp)

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. oracle ojdbc 版本须对应,否则日期字段查询结果与实际值可能不一致

    1. 数据库版本:select * from v$version; 2. 版本对应:

  9. numpy数组的索引和切片

    numpy数组的索引和切片 基本切片操作 >>> import numpy as np >>> arr=np.arange(10) >>> arr ...

  10. 洛谷 P1306 斐波那契公约数 题解

    题面 结论:gcd(F[n],F[m])=F[gcd(n,m)]; F[n]=a和F[n+1]=b F[n+2]=a+b,F[n+3]=a+2b,…F[m]=F[m?n?1]a+F[m?n]b F[n ...