题目如下:

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. 【HANA系列】SAP HANA 2.0 SPS00 SDA(Smart Data Access)连接Hadoop

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA 2.0 SPS ...

  2. JAVA 编程思想一

    1: 动态绑定和静态绑定 使用private或static或final修饰的变量或者方法,使用静态绑定.而虚方法(可以被子类重写的方法)则会根据运行时的对象进行动态绑定: 静态绑定使用类信息来完成,而 ...

  3. java script 的注释与分号

    // 单行注释 /**/多行注释 在js 中 变量.函数和操作符都是区分大小写的 什么是标识符 变量.函数.属性的名字.或者函数的参数. 变量的命名规范:不能以数字开头. 变量声明: var  nam ...

  4. [转帖]黑客通过 Rootkit 恶意软件感染超 5 万台 MS-SQL 和 PHPMyAdmin 服务器

    黑客通过 Rootkit 恶意软件感染超 5 万台 MS-SQL 和 PHPMyAdmin 服务器 https://www.cnbeta.com/articles/tech/852141.htm 病毒 ...

  5. 洛谷P1600 天天爱跑步——题解

    题目传送 首先要考虑入手点.先考虑一个一个玩家处理,显然不加优化的话,时间复杂度是O(n)的.发现对于玩家路径上的点都有一个观察员,一个都不能忽视,看起来是很难优化了.在做题时,发现一个思路很难想,就 ...

  6. Office批量授权(VL)版本和激活方法

    Office 2010 Office 2010中文专业增强版 32位 文件名: SW_DVD5_Office_Professional_Plus_2010w_SP1_W32_ChnSimp_CORE_ ...

  7. Codeforces Round #587 (Div. 3)

    https://codeforces.com/contest/1216/problem/A A. Prefixes 题意大概就是每个偶数位置前面的ab数目要相等,很水,被自己坑了 1是没看见要输出修改 ...

  8. Python基础学习——文件操作、函数

    一.文件操作 文件操作链接:http://www.cnblogs.com/linhaifeng/articles/5984922.html(更多内容见此链接) 一.对文件操作流程 打开文件,得到文件句 ...

  9. IE6兼容笔记

    1.IE6中,元素右浮动的时候前面不能有文本或内联元素,否则会换行独占一行 解决办法:将浮动元素放到文本或内联元素前面,大都在制作新闻列表的时候会遇到这种问题. 未完,待续!

  10. mysql登录密码错误,以及设置密码

    1.输了几次,密码都错误,忘记了... 2.编辑mysql安装文件夹(D:\mysql-5.7.26-winx64)下的my.ini文件,mysqld标签下输入 skip-grant-tables,字 ...