【LeetCode】392. Is Subsequence 解题报告(Python)
【LeetCode】392. Is Subsequence 解题报告(Python)
标签: LeetCode
题目地址:https://leetcode.com/problems/is-subsequence/description/
题目描述:
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ace” is a subsequence of “abcde” while “aec” is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, … , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
题目大意
有两个字符串s和t,其中s是短字符串,t是长字符串。判断s是不是t的子字符串。注意,这里应该是保留相对顺序的子字符串,也就是在s中出现的两个字符a,b应该在s中t中的相对次序相同。
解题方法
其实这个题就考了一个相对顺序。s比较短,而t很长,那么尽量就对t进行一次遍历最好。可以使用一个队列保留s的每个元素,这样对t进行遍历,如果遍历到的元素和队列的头元素相等,那么队列出头元素。这样最后返回队列是否为空即可。
class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
queue = collections.deque(s)
for c in t:
if not queue: return True
if c == queue[0]:
queue.popleft()
return not queue
如果不使用队列的话,可以使用两个指针,一个作为s的索引,一个作为t的索引。如果在t中找到了s的元素,把s的指针右移,否则把t的指针右移。
这个竟然比上面的更慢??不懂为什么。
class Solution(object):
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
si, ti = 0, 0
while si < len(s) and ti < len(t):
if t[ti] == s[si]:
si += 1
ti += 1
return si == len(s)
方法二:
二分查找,留给二刷。
日期
2018 年 3 月 15 日 –雾霾消散,春光明媚
【LeetCode】392. Is Subsequence 解题报告(Python)的更多相关文章
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- SQL-增、删、改操作
#查看表 select * from `竟企区域数据分析` #在表第一列新增名为"年月"的列alter table `竟企区域数据分析` add column 年月 varchar ...
- (转载)java排序实现
Java实现几种常见排序方法 日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 冒泡排序是一种简单的排序算法. ...
- Hive(十三)【Hive on Spark 部署搭建】
Hive on Spark 官网详情:https://cwiki.apache.org//confluence/display/Hive/Hive+on+Spark:+Getting+Started ...
- [php代码审计] Typecho 1.1 -反序列化Cookie数据进行前台Getshell
环境搭建 源码下载:https://github.com/typecho/typecho/archive/v1.1-15.5.12-beta.zip 下载后部署到web根目录,然后进行安装即可,其中注 ...
- Linux学习 - 文件包处理命令
一.搜索文件find find [搜索范围] [匹配条件] (1) -name(名字查找) <1> find /etc -name init 查找/etc下以 "in ...
- LVS nat模型+dr模型
nat模型 在 rs1 和 rs2 安装httpd 并配置测试页,启动 [root@rs1 ~]# yum install httpd -y[root@rs1 ~]# echo "thi ...
- 【HarmonyOS】【DevEco Studio】NOTE02 :Create a “Hello World ”Application
Author:萌狼蓝天 StudyTime:2021/12/06 Version:3.0 Beta1 包结构 src | --> resource 资源文件目录 | --> layout/ ...
- python f-strings !表达式
近期看一些框架的文档时发现, python的f-strings有f"{xxx!r}"的写法, 就官网看了一波文档, 特此记录一下, 顺便完善一下f-strings的使用 f-str ...
- 转:苹果iphone APP界面设计尺寸官方版
苹果iphone APP界面设计尺寸官方版
- Java8 函数式接口 @FunctionalInterface以及常用Consumer<T>、Supplier<T>、Function<T, R>、Predicate<T>总结
首先看看什么是Lambda 表达式 Lambda是一个匿名函数,我们可以把Lambda表达式理解为一段可以传递的代码(将代码像数据一样传递):最简单的Lambda表达式可由逗号分隔的参数列表.-> ...