Lintcode392 Is Subsequence solution 题解
【题目描述】
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).
给定一个字符串s和一个字符串t,检查s是否是t的子序列。
可以认为,你可以假设在s和t中只有小写的英文字母。t是一个长(长度~ = 500,000)的字符串,s是一个短字符串(< = 100)。
一个字符串的子序列是一个新的字符串,它是由原来的字符串通过删除一些字符而形成的,而不影响其余字符的相对位置。(即“ace”是“abcde”的子序列,而“aec”则不是)。
【题目链接】
www.lintcode.com/en/problem/is-subsequence/
【题目解析】
首先想到的方法是,使用两个队列分别保存两个字符串中的各个字符,依次比较队首元素,若两个队列的队首元素相同,则表示到目前为止,s的前x个字符可以匹配为t的子序列;如果两个队列的队首元素不相同,则将t队列的首元素出队列,继续比较.......
当两个队列中有一个队列为空时,上述比较结束,此时判断s队列是否为空,若为空表示s的各个字符都匹配到了t中的字符,因此s是t的子序列;若s队列不为空,表示s字符串中有某些字符没有办法和t字符串无法匹配,因此s不是t的子序列。
【参考答案】
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Lintcode392 Is Subsequence solution 题解的更多相关文章
- Lintcode397 Longest Increasing Continuous Subsequence solution 题解
[题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...
- CF742B Arpa's obvious problem and Mehrdad's terrible solution 题解
Content 有一个长度为 \(n\) 的数组,请求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的数对 \((i,j)\) 的个数.其中 \(\oplus\) 表示 ...
- Lintcode360 Sliding Window Median solution 题解
[题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...
- Lintcode249 Count of Smaller Number before itself solution 题解
[题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...
- Lintcode248 Count of Smaller Number solution 题解
[题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, value from ...
- Lintcode247 Segment Tree Query II solution 题解
[题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...
- Lintcode245 Subtree solution 题解
[题目描述] You have two every large binary trees:T1, with millions of nodes, and T2, with hundreds of no ...
- Lintcode227 Mock Hanoi Tower by Stacks solution 题解
[题目描述] In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes w ...
- Lintcode223 Palindrome Linked List solution 题解
[题目描述] Implement a function to check if a linked list is a palindrome. 设计一种方式检查一个链表是否为回文链表. [题目链接] w ...
随机推荐
- Linux知识积累(3)$()和${}和$(())和(())
$()和${}和$(())和(()) $()和${}的用法:在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换用(command substitution)的.而 $( ...
- 新概念英语(1-1)Excuse me!
Excuse me!Whose handbag is it? A:Excuse me! B:Yes? A:Is this your handbag? B:Pardon? A:Is this your ...
- Linux上 ps 命令的用法
ps a 显示现行终端机下的所有程序,包括其他用户的程序.2)ps -A 显示所有程序. 3)ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示. 4)ps -e 此 ...
- python实现 多叉树 寻找最短路径
完全原创,能力有限,欢迎参考,未经允许,请勿转载 ! 完全原创,能力有限,欢迎参考,未经允许,请勿转载 ! 完全原创,能力有限,欢迎参考,未经允许,请勿转载 ! 完全原创,能力有限,欢迎参考,未经允许 ...
- Vue框架
Vue框架 环境: windows python3.6.2 Vue的cdn: <script src="https://cdn.jsdelivr.net/npm/vue"&g ...
- 1114innodb的统计信息对optimizer成本预估影响实例 CARDINALITY
转自 https://www.cnblogs.com/olinux/p/5140615.html 转自 https://yq.aliyun.com/articles/174906?spm=5176 ...
- leetcode 717. 1-bit and 2-bit Characters -easy
https://leetcode.com/problems/1-bit-and-2-bit-characters/description/ We have two special characters ...
- [原创]手把手教你写网络爬虫(5):PhantomJS实战
手把手教你写网络爬虫(5) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 大家好!从今天开始,我要与大家一起打造一个属于我们自己的分布式爬虫平台,同时也会对涉及到的技术进行详细介绍.大 ...
- Python的hasattr() getattr() setattr() 函数使用方法详解 (转)
来自:https://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOO ...
- servlet学习总结
一.web工程结构 1.HTTP协议(hyper text transfer protocol)(超文本传输协议) 机制:请求/响应 机制(request/response)(HttpServletR ...