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. node,npm,webpack,vue-cli模块化编程安装流程

    首先什么都不要管,先装环境. pip是万能的!!! 安装node: pip3 install node 安装npm:   pip3 install npm 安装webpack: npm install ...

  2. java类的实例化顺序

    1. 父类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 2. 子类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 3. 父类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行 4 ...

  3. 同步nginx日志到s3 bulket

    1.此处用的是增量同步 #!/bin/bash rm -rf /var/log/nginx/access.log.*.* local_host="`hostname --i`" a ...

  4. slideshare文档下载

    if [ x"$1" = x1 ]; then for i in {1..46}; do url_i="https://image.slidesharecdn.com/b ...

  5. Oracle存储过程案例集合

    注:使用的工具为PLSQL Developer 壹.while简单使用(替换字符串中的字符,和REPLACE效果一样) 注: 这里没有使用REPLACE函数 1.建立存储过程 CREATE OR RE ...

  6. Window命令行切换命令

    Windows 命令行切换目录 特别注意:切换到其它盘符不需要 cd 命令 1. 切换到 C 盘根目录 打开终端 cmd 后,输入cd C:\(一定要加上后面的反斜扛) 2.切换到 C 盘子目录 打开 ...

  7. 四十二、LAMP与LNMP web架构深度优化实战-第一部

    1.nginx.conf配置文件基本参数优化 1.1 隐藏nginx header内版本号信息 一些特定的系统及服务漏洞一般都和特定的软件版本号有关,我们应尽量隐藏服务器的敏感信息(软件名称及版本等信 ...

  8. Linux实验总结(第二周)

    测试一--vi 每个.c一个文件,每个.h一个文件,文件名中最好有自己的学号 用Vi输入图中代码,并用gcc编译通过 在Vi中使用K查找printf的帮助文档 提交vi编辑过程截图,要全屏,包含自己的 ...

  9. python 并发执行

    并发执行, 精简代码. 适用python2 和python3 # -*- encoding:utf-8 -*- from threading import Thread from multiproce ...

  10. day56-mysql-其他查询-面试题-重点

    一.重点 面试题 1. 临时表查询: -- 查询高于本部门平均工资的人员 #1.先查询部门人员的平均工资 SELECT dept_id,AVG(salary)as sal from person GR ...