Question

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?

Credits:

Special thanks to @pbrother for adding this problem and creating all test cases.

Solution

依次匹配就好了。

Code

class Solution {
public:
bool isSubsequence(string s, string t) {
if (s.length() > t.length())
return false;
if (s.length() == 0)
return true;
int index = 0;
for (int i = 0; i < t.length(); i++) {
if (t[i] == s[index])
index++;
}
if (index == s.length())
return true;
else
return false;
}
};

LeetCode——Is Subsequence的更多相关文章

  1. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  2. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  3. LeetCode "Wiggle Subsequence" !

    Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, ...

  4. [LeetCode] Is Subsequence 题解

    前言 这道题的实现方法有很多,包括dp,贪心算法,二分搜索,普通实现等等. 题目 Given a string s and a string t, check if s is subsequence ...

  5. LeetCode "Is Subsequence"

    There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) co ...

  6. leetcode 376Wiggle Subsequence

    用dp解 1)up定义为nums[i-1] < nums[i] down nums[i-1] > nums[i] 两个dp数组, up[i],记录包含nums[i]且nums[i-1] & ...

  7. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  8. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  9. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. 苏宁易购Android架构演进史

    互联网后端架构 https://mp.weixin.qq.com/s/5lDXjMh6ghQNi4E7qQIEEg 互联网后端架构 10月9日 摘要 移动青铜时代(2012-2014) 时代特点: 移 ...

  2. 有些有IP的项目,公司不至于测试不行砍项目,但是会砍项目组,把IP收回交给别的团队做(因为一旦一测数据太差,公司(投资人)会判断在二测的时候数据能提升到什么样。说白了就是历史信用问题)

    作者:匿名用户链接:https://www.zhihu.com/question/309778033/answer/579761064来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  3. Redis常见操作

    1. 对于key的所有操作 del key1 key2 … keyn 作用:删除1个或者多个键返回值:不存在的key忽略掉,返回真正删除的key的数量 rename key newkey 作用:给ke ...

  4. 对Numpy数组按axis运算的理解

    Python的Numpy数组运算中,有时会出现按axis进行运算的情况,如 >>> x = np.array([[1, 1], [2, 2]]) >>> x arr ...

  5. 前端 javascript 数据类型 数字

    1.数字(Number) JavaScript中不区分整数值和浮点数值,JavaScript中所有数字均用浮点数值表示. 转换: parseInt(..)    将某值转换成数字,不成功则NaN pa ...

  6. 微信对接HIS——微信可查检验结果

    患者仅仅要关注医院官方微信,不管身处何地,输入自己预留在医院的电话号码.检验单的条码号,就能够了解检验结果. 医院信息系统在提供病人数据信息前,会对查询方做身份认证和安全防护检測,录入患者挂号时预留的 ...

  7. ORACLE USERENV函数

    ORACLE USERENV函数 USERENV返回关于当前会话的信息.此信息可以用于编写一个应用程序特定的审计跟踪表或确定特定于语言的角色目前使用的会话. 参数 功能 CLINET_INFO 返回最 ...

  8. (转)VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径说明

      $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置. $(References) 以分号分隔的引用列表被添 ...

  9. oracle 数据库误删数据,误删表的恢复

    1.某表的数据误删了,那么可以查询这个表某一时间节点之前的数据,并放到一个新建的表里. create table temptable as select * from t_billdefi  as O ...

  10. sqlserver2008自动备份,自动删除较早的别分文件

    1.自动备份数据库 完成时候可以在维护计划中看到这个计划任务(xxxback),同事在作业里面会有个类似名称的作业(sql代理要启动,才能自动执行). 2.自动删除较早的备份文件 邮件选择上面的维护计 ...