392. Is Subsequence

Easy

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?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

package leetcode.easy;

public class IsSubsequence {
public boolean isSubsequence(String s, String t) {
int i = 0;
int j = 0;
for (; i < s.length() && j < t.length();) {
if (s.charAt(i) == t.charAt(j)) {
i++;
j++;
} else {
j++;
}
}
if (i == s.length()) {
return true;
} else {
return false;
}
} @org.junit.Test
public void test() {
System.out.println(isSubsequence("abc", "ahbgdc"));
System.out.println(isSubsequence("axc", "ahbgdc"));
}
}

LeetCode_392. Is Subsequence的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

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

  3. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  4. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  8. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. python的isinstance()函数

    以下是使用isinstance()函数的实例: a = isinstance(a,int) # 结果返回 True isinstance(a,str) # 结果返回 False 即:第1个参数是第2个 ...

  2. Hbase节点及原理

    1.Hbase架构:Clinet.Zookeeper.Master.RegionServer.Region.Store.MemStore.HLog 1.读过程 2.写过程 https://www.cn ...

  3. Acwing P283 多边形 题解

    Analysis 总体来说是一个区间DP 此题首先是一个环,要你进行删边操作,剩下的在经过运算得到一个最大值 注意事项: 1.删去一条边,剩下的构成一条线,相当于求此的最大值,经典区间DP该有的样子: ...

  4. P1853 投资的最大效益

    题目背景 约翰先生获得了一大笔遗产,他暂时还用不上这一笔钱,他决定进行投资以获得更大的效益.银行工作人员向他提供了多种债券,每一种债券都能在固定的投资后,提供稳定的年利息.当然,每一种债券的投资额是不 ...

  5. centos host-only网卡配置

    1.更改网卡配置 更改虚拟机/etc/sysconfig/network-scripts/ifcfg-enp0s3 文件,内容如下: TYPE=Ethernet PROXY_METHOD=none B ...

  6. CODE FESTIVAL 2017 qual C 题解

    传送门 \(A\) 咕咕 const int N=15; char s[N];int n; int main(){ scanf("%s",s+1),n=strlen(s+1); f ...

  7. AtCoder Grand Contest 017题解

    传送门 \(A\) 直接转移就是了 typedef long long ll; const int N=55; ll f[N][2];int a[N],n,p; int main(){ scanf(& ...

  8. vue.js 中使用(...)运算符报错的解决方法

    vue.js 中使用(...)运算符报错的解决方法 Syntax Error:Unexpected token(XX:X) }, computed:{ ...mapGetters([ 'pageSiz ...

  9. MySQL数据库入门——多实例配置

    前面介绍了相关的基础命令操作,所有的操作都是基于单实例的,mysql多实例在实际生产环境也是非常实用的,因为必须要掌握 1.什么是多实例 多实例就是一台服务器上开启多个不同的服务端口(默认3306), ...

  10. java中nextLine()与next()的区别

    java中的next()和nextLine()还是有很大区别的. next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自 ...