Description

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.

My program:

从s和t的首端开始遍历比较字符是否相等即可,如果不等,则增加在t中的下标j位置;如果相等,则同时增加s中下标i和t中下标j。如果t中的指标位置增长到了t的末尾,而s中的指标还没有增长的末尾,则返回false。如果s中的指标先增长到了末尾,则返回true。

class Solution {
public:
bool isSubsequence(string s, string t) {
int i = 0, j = 0;
if (s.empty()) return true;
while(i<s.size() && j<t.size())
{
if(s[i] == t[j])
{
++i;
++j;
}
else
++j;
}
if (i<s.size()) return false;
else return true;
}
};

Simple C++ code:

bool isSubsequence(string s, string t) {
int si= 0;
for(int ti = 0; ti < t.size() && si < s.size(); ti++) {
if(t[ti] == s[si]) si++;
}
return si == s.size();
}

Leetcode392. Is Subsequence的更多相关文章

  1. [Swift]LeetCode392. 判断子序列 | 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] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

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

  3. [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 ...

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

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

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

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

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

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

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  9. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. Spring入门程序-前端控制器配置器

    1,处理器的第二种配置方式 <!--配置handler --> <bean id="/FirstController" class="com.songy ...

  2. golang设计模式-成员变量赋值

    常见golang的struct赋值有两种: 1)定义变量同时初始化 val := &Options{ UID:int(1), } 2)先定义变量,再赋值 val := new(Options) ...

  3. 让你的saga更具有可伸缩性(Scaling NServiceBus Sagas)

    https://lostechies.com/jimmybogard/2013/03/26/scaling-nservicebus-sagas/ 当我们使用NServiceBus sagas (pro ...

  4. 转载:win10 下安装32位的Oracle 11g 客户端(问题:环境不满足最低要求)

    1. 在安装文件夹中,找 stage->cvu->cvu_prereq.xml文件. 2. 用记事本打开其xml文件进行编辑,加下面一段保存. <OPERATING_SYSTEM R ...

  5. 几种常用的json序列化和反序列化工具介绍

    一.前言 Json序列化和反序列化工作中会时常用到,也是目前数据交互的常用格式,Rest风格的接口加上json格式的数据交互,真的是天作之合. 目前Json字符与Json对象的相互转换方式有很多,接下 ...

  6. GyoiThon:基于机器学习的渗透测试工具

    简介 GyoiThon是一款基于机器学习的渗透测试工具. GyoiThon根据学习数据识别安装在Web服务器上的软件(操作系统,中间件,框架,CMS等).之后,GyoiThon为已识别的软件执行有效的 ...

  7. chromatic aberration

    https://github.com/keijiro/KinoFringe https://en.wikipedia.org/wiki/Chromatic_aberration 色差偏移 做神经病效果 ...

  8. Android EditText输入字数限制总结(包含中文输入内存溢出的解决方法)

    转载请注明,大飞:http://blog.csdn.net/rflyee/article/details/38856539 限定EditText输入个数的解决方式非常多,可是一般主要考虑两点.也就是处 ...

  9. SQL Server 高性能写入的一些经验总结

    转自:http://www.jb51.net/article/31162.htm 本篇博文将针对一些常用的数据库性能调休方法进行介绍,而且,为了编写高效的SQL代码,我们需要掌握一些基本代码优化的技巧 ...

  10. 【android相关】【问题解决】R.java文件丢失

    在进行android开发过程中,有时候,我们会遇到gen文件中R.java丢失的现象.重新build,或者clean工程,close并重新打开Project,但有时也没解决. 这可能是由于不小心把xm ...