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 sis 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?

题意:

给定串串s和t, 判断s是否是t的subsequence

思路:

指针count用来scan串串s

指针i用来scan串串t

从左至右,若t当前的字符等于s当前字符,count++

若count等于s.length(),说明s中的每个字符都包含在串串t中,最终说明s是t的subsequence

代码:

 class Solution {
public boolean isSubsequence(String s, String t) {
if(s == null || s.length() == 0) return true;
int count = 0;
for(int i = 0; i<t.length() ; i++){
if(t.charAt(i) == s.charAt(count)){
count++;
if(count == s.length()){return true;}
}
}
return false;
}
}

[leetcode]392. Is Subsequence 验证子序列的更多相关文章

  1. LeetCode 392. 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 376. Wiggle Subsequence 摆动子序列

    原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers s ...

  3. 392 Is Subsequence 判断子序列

    给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...

  4. [leetcode] 392. Is Subsequence (Medium)

    原题 判断子序列 /** * @param {string} s * @param {string} t * @return {boolean} */ var isSubsequence = func ...

  5. LeetCode 392. Is Subsequence 详解

    题目详情 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 & ...

  6. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  7. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. 392. Is Subsequence

    392. Is Subsequence 水题,先是判断长度,长度t比s小,返回false,然后从左到右扫描t,然后同时扫描s,如果相同,s的index就往后拉一个,如果s的index等于s长度,返回t ...

  9. LeetCode:递增的三元子序列【334】

    LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i ...

随机推荐

  1. C++中关于class B:A与Class B::A问题

    一,class B:A为类的继承关系,即A类是B类的基类class <派生类名>:<继承方式><基类名>{<派生类新定义成员>}; 例如: #inclu ...

  2. Ubuntu登录系统失败的解决方案

    问题一: 只能用guest用户登录下,如何切换成普通用户登录 解决: 重启,同时按Esc建,直至进入到恢复模式下: 选择第一项,进入: 找到ro...那一行,把ro之后的删除,并把ro修改为rw si ...

  3. flask中的蓝图与红图

    内容: 1.flask中的蓝图 2.flask子域名实现 3.flask中的红图 1.flask中的蓝图 一个大型项目中视图比较多,如果仅仅是写在app.py中不方便管理,蓝图就可以做到分功能分目录结 ...

  4. c++官方文档-指针

    #include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...

  5. 温故而知新-正则单词和strlen

    1 正则表达式用\b表示单词的开始和结束 \bblog\b 正则查找blog这个单词 2 关于strlen的汉字问题 在utf8格式下  strlen('汉字')=6 在gbk格式下 strlen(' ...

  6. saltstack安装配置及常用命令

    1.salt安装及配置详解 https://www.cnblogs.com/lgeng/p/6567424.html centos7配置: https://www.jianshu.com/p/4c91 ...

  7. JAVA Spring 简单的配置和操作 ( 创建实体类, 配置XML文件, 调试 )

    < 1 > 实体类 Person package java_spring.modle; /** * 一个实体类( Person ) */ public class Person { pri ...

  8. HTML5 Canvas ( 圆和切点曲线的绘制 ) arc, arcTo

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. leetcode202

    public class Solution { private int SumSqares(int n) { //将一个数字的各个数位的值分开存储 var list = new List<int ...

  10. 使用Travis进行持续集成

    使用Travis进行持续集成 廖雪峰 持续集成:Continuous Integration,简称CI,意思是,在一个项目中,任何人对代码库的任何改动,都会触发CI服务器自动对项目进行构建,自动运行测 ...