【leetcode】1178. Number of Valid Words for Each Puzzle
题目如下:
With respect to a givenpuzzlestring, awordis valid if both the following conditions are satisfied:
wordcontains the first letter ofpuzzle.- For each letter in
word, that letter is inpuzzle.
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, whereanswer[i]is the number of words in the given word listwordsthat are valid with respect to the puzzlepuzzles[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^54 <= words[i].length <= 501 <= puzzles.length <= 10^4puzzles[i].length == 7words[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的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】996. Number of Squareful Arrays
题目如下: Given an array A of non-negative integers, the array is squareful if for every pair of adjacen ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
随机推荐
- CSS元素隐藏
{ display: none; /* 不占据空间,无法点击 */ } /*************************************************************** ...
- Shell编程、part4
本节内容 1. shell函数 2. shell正则表达式 shell函数 shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数.给这段代码起个名字称为函数名,后续可以直 ...
- 爬虫五之Selenium
Selenium 自动化测试工具,支持多种浏览器: 爬虫中主要用来解决JavaScript渲染问题. 用法详解 基本使用 声明浏览器对象 from selenium import webdriver ...
- 深入理解java:4. 框架编程
了解 Servlet 和 Filter Servlet(即servlet-api.jar) 是 J2EE 最重要的一部分,有了 Servlet 你就是 J2EE 了,J2EE 的其他方面的内容择需采用 ...
- Python pymysql对数据库的基础操作
示例数据库名demo,表名info select * from info; 查看该表数据 +----+-------+--------+-----+---------------------+---- ...
- 2019JAVA第五次实验报告
Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间2019/10/11 评分等级 实验四 类的继承 实验目的 理解抽象类与接口的使用: 了解包的作用,掌握包的设计方法. ...
- numpy将数组保存为文件
保存单个数组 np.save和np.load是读写磁盘数组数据的两个主要函数.默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为.npy的文件中的: 如果文件路径末尾没有扩展名.npy,则该扩展 ...
- Win7(64位)下安装Anaconda+Tensorflow(CPU)
一.安装Python 3.5 下载Anaconda网址:https://www.anaconda.com/download/ 安装:Anaconda3-4.2.0-Windows-x86_64.exe ...
- npm 关联 git包
npm 关联 git包 由于现在项目越做越多,很多公共的部分相互公用,需要尽可能早地提炼出来,这样便可以在其他项目进行引用,而不是每次建一个项目就需要进行拷贝,这样太痛苦了,因而想通过类似npm包管理 ...
- vue.js的v-bind
v-bind v-bind 主要用于属性绑定, html中的标签内: <div class="control-group"> <label class=&quo ...