【题目描述】

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的子序列。

【参考答案】

www.jiuzhang.com/solutions/is-subsequence/

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Lintcode392 Is Subsequence solution 题解的更多相关文章

  1. Lintcode397 Longest Increasing Continuous Subsequence solution 题解

    [题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...

  2. CF742B Arpa's obvious problem and Mehrdad's terrible solution 题解

    Content 有一个长度为 \(n\) 的数组,请求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的数对 \((i,j)\) 的个数.其中 \(\oplus\) 表示 ...

  3. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Lintcode245 Subtree solution 题解

    [题目描述] You have two every large binary trees:T1, with millions of nodes, and T2, with hundreds of no ...

  8. 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 ...

  9. Lintcode223 Palindrome Linked List solution 题解

    [题目描述] Implement a function to check if a linked list is a palindrome. 设计一种方式检查一个链表是否为回文链表. [题目链接] w ...

随机推荐

  1. tomcat增加处理线程数量

    修改server.xml <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" ma ...

  2. Python入门之Python在Win10环境下的配置(图文教程)

    请在Python官网下载Python2.7和Python3.6安装包,虽然最新的是3.6版本,但是建议两个包都安装,方便后期在IDE工具切换. Python官网:https://www.python. ...

  3. CWMP开源代码研究番外篇——博通方案

    声明:本篇文章来自于某公司Cable Modem产品的文档资料,源码来自于博通公司,只提供参考(为保护产权,本人没有源码). 前文曾提到会写一篇关于博通的tr069,那么福利来了.福利,福利,福利,重 ...

  4. Zookeeper通过java创建、查看、修改、删除znode

    本章主要介绍zookeeper如何使用,其实通过zkCli.cmd我们是可以执行一些操作的:声明:参考及转自<http://www.blogjava.net/BucketLi/archive/2 ...

  5. hdu1052 Tian Ji -- The Horse Racing---田忌赛马贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1052 题目大意: 田忌和齐王各有N匹马,判断怎样比赛,使田忌净胜场数最多. 思路: 一开始贪心出错, ...

  6. 找出一个文件夹下后缀名为.jpg的文件

    import os list1=os.lisdir('E//') #方法一列表推导式 list2=[i for i in list1 if i.endswith('.jpg')] #方法二for循环 ...

  7. OS模块文件操作一

    1          文件操作 1.1               OS模块 l  import os  #引入os模块 l  import os.path  #引入os下的path子模块 l  os ...

  8. C# 关键字替换

    /// <summary> /// 关键字替换 /// </summary> /// <param name="body"></param ...

  9. 三.SQL语句实例

    1.查询A表中存在而B表中不存在的数据 1.1 描述:表A中有一tel字段,表B中有一tel字段,两个字段存储的内容部分相同,现要查询A表tel字段中有而B表tel字段中没有的数据 1.2 有三个se ...

  10. C#之Excel操作

    下面的这几个方法是我在项目中经常用到的,欢迎大家批评指正 读取Excel表中的数据 第一种:功能丰富,速度慢 /// <summary> /// 从Excel读取数据 /// </s ...