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 Handler 的使用

    Android UI 操作是线程不安全的.我们只能在UI线程或者说主线程中修改UI.试想多个Thread操作同一个UI,可能引起不一致.UI 线程的主要工作是:UI界面更新显示,各个控件的交互等等.一 ...

  2. Yii框架2.0的视图和widgets表单的使用

    2.0的控制器的创建和action方法的添加和1.0的基本是一样的.但我发现他的widgets 的ActiveForm和1.1版本有些不一样的地方. 对于视图基本也是一样的,有render 和 ren ...

  3. shipyard 中文版安装 -- Docker web管理

    #本文使用markdown文档格式 #Docker web管理平台 #shipyard 中文版安装 #hipyard可对容器.镜像.仓库.docker节点进行管理的web系统 #+++++++++++ ...

  4. 理解java动态代理

    java动态代理是java语言的一项高级特性.在平时的项目开发中,可能很难遇到动态代理的案例.但是动态代理在很多框架中起着不可替代的作用,例如Spring的AOP.今天我们就聊一聊java动态代理的实 ...

  5. Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入

    所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入   1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...

  6. windows批处理初学贴出一些命令

    在cmd窗口中复制时,右键选标记,然后再选择此时选择区域就变白了.然后要么直接拖到要粘贴的地方,要么直接按回车存到剪贴板里. 1.循环导入文件夹下面的文件到数据库中 cd /d D:/Program ...

  7. MFC中对基于ODBC对数据ACCESS数据库的增删改查。

    在MFC中可以使用很多方法对数据库进行操作. 什么ODBC  什么ADO之类的,这里要介绍使用的ODBC这种方法,通过本文的阅读可以达初步掌握在MFC里面通过ODBC访问ACCESS数据库. 涉及到的 ...

  8. 微信小程序组件action-sheet

    操作反馈action-sheet:官方文档 Demo Code: Page({ data: { actionSheetHidden: true, actionSheetItems: ['item1', ...

  9. spring mvc 全局处理异常

    spring框架支持很多种全局处理异常的方式 一.Spring MVC处理异常有4种方式: (1)使用Spring-MVC提供的SimpleMappingExceptionResolver: (2)实 ...

  10. Django的orm练习---多表查询

    Django的orm练习---多表查询 表关系如下 表结构 : from django.db import models # Create your models here. # 多对多-----&g ...