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 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的更多相关文章
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- CF724D. Dense Subsequence[贪心 字典序!]
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
随机推荐
- IDEA设置提示生成序列化ID
背景: 实现Serializable接口的类,没有提示生成序列化ID 解决问题: 1.FIle->Settings->Editor->inspections 2.点击java-> ...
- 对日开发中 PG , PL , SE , PM 是什么
PG(ProGramer)指程序员. 这类人才在企业中所占数量最多,通常占到整个项目员工数的70%,也是企业中最紧缺的一类职位,一般为具有专业知识的软件工程技术人员. PL(project leade ...
- Kubernetes 学习13 kubernetes pv pvc configmap 和secret
一.概述 1.我们在pvc申请的时候未必就有现成的pv能正好符合这个pvc在申请中指定的条件,毕竟上一次的成功是我们有意设定了有一些满足有一些不满足的前提下我们成功创建了一个pvc并且被pod绑定所使 ...
- easyui181版本使用记录
easyui181版本下载地址 简单java+easyui181版本demo下载 注意前端页面的ajax请求路径对应后端 如果再加强样式可使用adminLTE
- PHP 高手博客网站集合
风雪之隅-Laruence的博客 韩天峰(Rango)的博客 我的志愿是做一个校长 张宴的博客 - Web系统架构与底层研发 沈逸的个人站点 博学无忧 - 信海龙的博客
- PHPStorm启动问题--Failed to load JVM DLL
一.问题 启动PHPStorm时突然遇到PHPStorm无法启动的问题,提示:Failed to load JVM DLL,如图所示:
- 洛谷 P1006 传纸条 题解
P1006 传纸条 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法 ...
- 在Matlab终止程序后的异常
有时终止Matlab程序后,其内部指令会执行异常,出现不识别命令函数的情形.我遇到过执行sum命令出错的问题.退出程序,重启后正常.
- (6)Go函数和函数式编程
一.Go函数 函数是组织好的.可重复使用的.用于执行指定任务的代码块.本文介绍了Go语言中函数的相关内容. Go语言中支持函数.匿名函数和闭包,并且函数在Go语言中属于"一等公民" ...
- jmeter之timer --笔记一
简介:测试过程中需要用到time进行造数据测试,需要各种年月日,或者未来时间,就像python中的time和datetime 1.jmeter中timer,使用—time()函数 1.1 timeSh ...