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. Java 导入证书

    在这个目录下: ${JAVA_HOME}\jre\lib\security 执行 keytool -importcert -trustcacerts -file yourcerfile.pem -al ...

  2. linux常用命令解析

    linux下一些注意事项 1. linux下严格区分大小写 ls 简述:列出文件或目录列表. -> ls 默认列出当前目录下的所有文件. -> ls -l(long)以长格式查看文件. - ...

  3. 第10课 struct 和 union 分析

    1. struct的小秘密 (1)C语言中的struct可以看作变量的集合 (2)struct的问题——空结构体占用多的内存? [实例分析]空结构体的大小 #include <stdio.h&g ...

  4. android 系统架构简介

    Android系统采取的是分层的架构,根据官方文档提供的架构图,我们将android的系统架构分成5层,如图: 1.Application Framework (应用框架) application f ...

  5. jshint在bat批处理中闪退,代码中无法调用的问题

    先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更

  6. c++官方文档-动态内存

    #include<iostream> #include <new> using namespace std; int main() { /** * 动态内存 * url: ht ...

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

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

  8. sqoop1 使用测试

    hive导入数据到mysql最简单的方式就是从hdfs直接读取hive表文件导入mysql,当然这需要知道数据表保存的目录 如果能直接从表到表的导入,无需路径,当然是最好了 1.需要下载合适的hive ...

  9. leetcode944

    public class Solution { public int MinDeletionSize(string[] A) { ; ; j < A[].Length; j++) { ; i & ...

  10. 字符串和JSON对象互转的方法

    采用Ajax的项目开发过程中,经常需要将JSON格式的字符串返回到前端,前端解析成JS对象(JSON ).字符串转JSON对象 1.eval方式解析.function strToJson(str){ ...