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. ylbtech-dbs-m-ele(饿了么)

    ylbtech-dbs:ylbtech-m-ele(饿了么) -- =============================================-- DatabaseName:Ele - ...

  2. 怎样用bat批量重命名文件夹和文件

    很早以前本人写过重命名文件夹的文章,发现其中稍有不完善的地方,其主要功能在文件夹名前统一加上字符,或者在文件夹名后统一加上字符,有网友反应功能太单一.今天我又仔细研究了一下bat批处理代码,分别能完全 ...

  3. 使用.reg文件删除暴风影视库图标和注册信息

    暴风播放器安装后会自动安装一个叫暴风影音库的软件,在你的电脑资源管理器中增加了一个“暴风影视库”的图标.看着很烦,删起来还比较麻烦,于是搜索了相关资源,自己写了个注册表处理文件,方便大家一键删除之. ...

  4. replace的用法

    http://blog.sina.com.cn/s/blog_9ed9ac7d0101ec1f.html replace 语句 如果存在,更新,否则,插入在使用REPLACE时,表中必须有唯一索引,而 ...

  5. Oracle database wrc运行报错ORA-15557

    [oracle@host capture]$ wrc system/oracle@db1 REPLAYDIR=/home/oracle/cap_dir/ Workload Replay Client: ...

  6. docker集群——介绍Mesos+Zookeeper+Marathon的Docker管理平台

    容器为用户打开了一扇通往新世界的大门,真正进入这个容器的世界后,却发现新的生态系统如此庞大.在生产使用中,不论个人还是企业,都会提出更复杂的需求.这时,我们需要众多跨主机的容器协同工作,需要支持各种类 ...

  7. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-电机实际运行距离跟给定距离不一致怎么办,如何设置Scaling Factor

    有时候,让电机从0度转到绝对的360度,有时候会出现电机实际转动更多或者更少的情况.   一般是电机的编码器的Scaling Factor Numerator数值不对导致的,数值越小,则同比转过角度越 ...

  8. 【DB2】对两列分组之后判断另外一列是否有重复

    建立表数据如下: ),sex ),sex_nm ),OWER ),TYPE ),TYPE_NM )); ,','水果'), (,','水果'), (,','水果'), (,','水果'), (,',' ...

  9. XPage的使用示范例子

    代码地址如下:http://www.demodashi.com/demo/12975.html XPage [[api][apisvg]][api] 一个非常方便的fragment页面框架 特点 支持 ...

  10. springMVC4(5)RestTemplate控制层单元測试

    在前面我们进行web測试,总要在游览器进行.数据组装.请求方法更给等都极为麻烦. RestTemplate是Spring提供的一个web层測试模板类,我们能够通过RestTemplate在client ...