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 ...
随机推荐
- 从Mybatis源码理解jdk动态代理默认调用invoke方法
一.背景最近在工作之余,把开mybatis的源码看了下,决定自己手写个简单版的.实现核心的功能即可.写完之后,执行了一下,正巧在mybatis对Mapper接口的动态代理这个核心代码这边发现一个问题. ...
- python实现归并排序,归并排序的详细分析。
学习归并排序的过程是十分痛苦的.它并不常用,看起来时间复杂度好像是几种排序中最低的,比快排的时间复杂度还要低,但是它的执行速度不是最快的.很多朋友不理解时间复杂度低为什么运行速度不一定快,这个不清楚的 ...
- HTNL表单详解
HTML表单 表单的结构 表单的标签:<form> </form> 常用属性 Name , method(get,post), action(服务器的接收的页面如:reg.ph ...
- semver(Semantic Versioning)
Based on semver, you can use Hyphen Ranges X.Y.Z - A.B.C 1.2.3-2.3.4 Indicates >=1.2.3 <=2.3.4 ...
- 并发容器和框架之ConcurrentHashMap
了解HashMap的人都知道HashMap是线程不安全的(多线程下的put方法达到一定大小,引发rehash,导致闭链,最终占满CPU),同时线程安全的HashTable效率又令人望而却步(每个方法都 ...
- POJ-1789 Truck History---最小生成树Prim算法
题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...
- VueJs(2)---VueJs开发环境的搭建和讲解index.html如何被渲染
VueJs开发环境的搭建和讲解初始框架 有关如何搭建vue.js框架我这看了一篇文章,自己也根据它进行搭建环境. 文章地址:vue.js2.0实战(1):搭建开发环境及构建项目 接下来对初始的框架进行 ...
- jQuery系列 第二章 jQuery框架使用准备
第二章 jQuery框架使用准备 2.1 jQuery框架和JavaScript加载模式对比 jQuery框架的加载模式 <script> window.onload = function ...
- Sevrlet 工作原理解析-转
从 Servlet 容器说起 要介绍 Servlet 必须要先把 Servlet 容器说清楚,Servlet 与 Servlet 容器的关系有点像枪和子弹的关系,枪是为子弹而生,而子弹又让枪有了杀伤力 ...
- Unity3D UGUI 自动调节大小
可添加以下组件 组件包含的两个枚举参数,可以自行设定适应方式. 例如一个Text UI元素,当文字过多的时候他不会自动增加高度而导致文字不能完全显示,这时候就可以挂载这个组件,如上图设置参数,就可以自 ...