【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/number-of-matching-subsequences/description/
题目描述:
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of S.
Example :
Input:
S = "abcde"
words = ["a", "bb", "acd", "ace"]
Output: 3
Explanation: There are three words in words that are a subsequence of S: "a", "acd", "ace".
Note:
- All words in words and S will only consists of lowercase letters.
- The length of S will be in the range of [1, 50000].
- The length of words will be in the range of [1, 5000].
- The length of words[i] will be in the range of [1, 50].
题目大意
找出words里面有多少个S的子序列。注意,子序列可以不连续。
解题方法
最开始想的肯定是DP了,但是这种思路想歪了,因为不同words之间好像没有什么转移方程。。
现在又学到了一个新的判断是不是子序列的方法,使用字典保存s中字母所有索引,然后遍历word寻找索引。
对于word的每个位置的字符,同时用个prev变量保存遍历S时已经到达哪个位置了,然后从字典中寻找这个字符是否存在prev 后面出现过。很巧妙。
时间复杂度是O(S) + O(WLlog(S)),空间复杂度是O(S).
代码如下:
class Solution(object):
def numMatchingSubseq(self, S, words):
"""
:type S: str
:type words: List[str]
:rtype: int
"""
m = dict()
def isMatch(word, d):
if word in m:
return m[word]
prev = -1
for w in word:
i = bisect.bisect_left(d[w], prev + 1)
if i == len(d[w]):
return 0
prev = d[w][i]
m[word] = 1
return 1
d = collections.defaultdict(list)
for i, s in enumerate(S):
d[s].append(i)
ans = [isMatch(word, d) for word in words]
return sum(ans)
参考资料:
https://www.youtube.com/watch?v=l8_vcmjQA4g
日期
2018 年 9 月 25 日 —— 美好的一周又开始了,划重点,今天是周二
【LeetCode】792. Number of Matching Subsequences 解题报告(Python)的更多相关文章
- leetcode 792. Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- 792. Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- LeetCode 792. 匹配子序列的单词数(Number of Matching Subsequences)
792. 匹配子序列的单词数 792. Number of Matching Subsequences 相似题目 392. 判断子序列
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
随机推荐
- C语言 fastq文件转换为fasta文件
目前只能处理短序列,若要处理长序列,可按照https://www.cnblogs.com/mmtinfo/p/13036039.html的读取方法. 1 #include <stdio.h> ...
- java的缓冲流及使用Properties集合存取数据(遍历,store,load)
缓冲流 概述 字节缓冲流:BufferedInputStream,BufferedOutputStream 字符缓冲流:BufferedReader,BufferedWriter 缓冲流原理 缓冲区是 ...
- 巩固javaweb第三天
巩固内容: HTML 标题 HTML 标题(Heading)是通过<h1> - <h6> 标签来定义的. HTML 段落 HTML 段落是通过标签 <p> 来定义的 ...
- 生产调优4 HDFS-集群扩容及缩容(含服务器间数据均衡)
目录 HDFS-集群扩容及缩容 添加白名单 配置白名单的步骤 二次配置白名单 增加新服务器 需求 环境准备 服役新节点具体步骤 问题1 服务器间数据均衡 问题2 105是怎么关联到集群的 服务器间数据 ...
- A Child's History of England.6
It was a British Prince named Vortigern who took this resolution, and who made a treaty of friendshi ...
- 【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations: add and find. add ...
- Netty实现Socket
Netty实现Socket 从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念 ...
- 快速上手git gitlab协同合作
简单记录,整理. 摘要 为方便大家快速上手Git,并使用Gitlab协同合作,特编写此手册,手册内容不会太丰富与深入.主要包含如下内容: Git 使用教程1.1 安装1.2 常用命令1.3 版本控制1 ...
- VFL
VFL 1. 概念 VFL全称是Visual Format Language,翻译过来是"可视化格式语言" VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 2. ...
- 【编程思想】【设计模式】【行为模式Behavioral】策略模式strategy
Python版 转自https://github.com/faif/python-patterns/blob/master/behavioral/strategy.py #!/usr/bin/env ...