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?

class Solution {
public boolean isSubsequence(String s, String t) {
if (s == null || s.length() == 0) {
return true;
}
int index = 0;
for (char ch : t.toCharArray()) {
if (index < s.length() && s.charAt(index) == ch) {
index += 1;
}
}
return index == s.length();
}
}

[LC] 392. Is Subsequence的更多相关文章

  1. 392. Is Subsequence

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

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

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

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

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

  5. 392 Is Subsequence 判断子序列

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

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

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

  7. LeetCode 392. Is Subsequence 详解

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

  8. LeetCode赛题392---- Is Subsequence

    392. Is Subsequence Given a string s and a string t, check if s is subsequence of t. You may assume ...

  9. LeetCode_392. Is Subsequence

    392. Is Subsequence Easy Given a string s and a string t, check if s is subsequence of t. You may as ...

随机推荐

  1. Serverless 公司的远程团队沟通策略

    本文系译文,Serverless 团队分散在全球各地,本文介绍我们如何管理沟通策略和远程协作. 原作者:FelixDesroches 译者:Aceyclee 首先向不了解我们的人说明一下,Server ...

  2. Python—后台运行(nohup 、&、 2>&1详解)

    一.脚本文件(test.py) # -*- coding: UTF-8 -*- import time print("hello"," python") os. ...

  3. 禁止网站F12和查看源码

    window.onload=function(){ document.onkeydown=function(){ var e=window.event||arguments[0]; if(e.keyC ...

  4. 字符串中子序列出现次数(dp)

    躲藏 链接:https://ac.nowcoder.com/acm/problem/15669来源:牛客网 题目描述 XHRlyb和她的小伙伴Cwbc在玩捉迷藏游戏. Cwbc藏在多个不区分大小写的字 ...

  5. Powershell 中的管道

    管道 上个命令中的输出,通过管道作为下个命令的输入.Linux中的管道传递的是text,但ps中传递的是object.但是命令究竟返回的是什么类型呢?以下命令回答了这个问题: get-service ...

  6. object detection模型转换成TensorFlow Lite,在Android应用

    环境 tensorflow = 1.12.0 bazel = 0.18.1 ubuntu = 16.04 python = 3.6.2 安装 bazel (0.18.1) 如果tensorflow是1 ...

  7. c指针(2)

    #include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct LNode { cha ...

  8. 吴裕雄--天生自然Linux操作系统:Linux常用命令大全

    系统信息 arch 显示机器的处理器架构 uname -m 显示机器的处理器架构 uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / DMI) ...

  9. mysql之存储过程(二)

    1.批量根据复杂的SQL查询结果插入到新表 DELIMITER && CREATE PROCEDURE settlePADTEST() begin        DECLARE c_s ...

  10. Snapchat欲联手亚马逊推扫一扫功能,社交应用营收来源将有大变化?

    当下的社交应用,已经不能完全仅用"社交"的标签进行定义.因为目前的社交应用不仅承载着大众的喜怒哀乐和沟通指责,更在逐渐打造起一个连接多方的生态系统.甚至只从自身的营收.利润出发,社 ...