【leetcode】1178. Number of Valid Words for Each Puzzle
题目如下:
With respect to a givenpuzzle
string, aword
is valid if both the following conditions are satisfied:
word
contains 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 listwords
that 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^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的更多相关文章
- 【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 ...
随机推荐
- 采用WPF技术开发截图程序
前言 QQ.微信截图功能已很强大了,似乎没必要在开发一个截图程序了.但是有时QQ热键就是被占用,不能快速的开启截屏:有时,天天挂着QQ,领导也不乐意.既然是程序员,就要自己开发截屏工具,功能随心所欲 ...
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现
一.网络爬虫的定义 网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛. 网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一 ...
- (转)在Kubernetes集群中使用JMeter对Company示例进行压力测试
背景 压力测试是评估应用性能的一种有效手段.此外,越来越多的应用被拆分为多个微服务而每个微服务的性能不一,有的微服务是计算密集型,有的是IO密集型. 因此,压力测试在基于微服务架构的网络应用中扮演着越 ...
- python 并发编程 多线程 目录
线程理论 python 并发编程 多线程 开启线程的两种方式 python 并发编程 多线程与多进程的区别 python 并发编程 多线程 Thread对象的其他属性或方法 python 并发编程 多 ...
- 交换机安全学习笔记 第八章 针对POE的攻击
POE即 Power over Ethernet 借助于以太网供电.最初为了IP电话,目前主要用于功耗小于15.4w的设备例如Ap和视频监控设备.并且简化了相关设备的电力线布线. 英文缩写注释:PSE ...
- 第八周课程报告&&实验报告六
Java实验报告 班级 计科一班 学号 20188390 姓名 宋志豪 实验四 类的继承 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法. 实验要求 练习捕获异常.声明异常. ...
- PostgreSQL INSERT ON CONFLICT不存在则插入,存在则更新
近期有一个需求,向一张数据库表插入数据,如果是新数据则执行插入动作,如果插入的字段和已有字段重复,则更新该行对应的部分字段 1. 创建测试表 create table meta_data ( id s ...
- easyUI关键(常见)组件详解
一.easyUI 相关介绍 1.EasyUI 是前端框架,封装大量 css和封装大量 JS 2.使用前端框架时,给标签定义class 属性,就会有样式和脚本功能了(class属性对应了相关封装过的cs ...
- zabbix 监控redis python3脚本
一:安装redis-python模块 wge thttps://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz tar xf r ...
- tarjan算法求无向图的桥、边双连通分量并缩点
// tarjan算法求无向图的桥.边双连通分量并缩点 #include<iostream> #include<cstdio> #include<cstring> ...