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 ...
随机推荐
- c++ templat乱测
该上机实验环境 linux mint IDE:qt5.11 代码复制到windows下vs2017报错,提示char* 类型不能直接赋值字符串 在linux mint下可以运行,测试目的:检验复 ...
- kafka2.12 集群搭建
前提: 1.下载 kafka http://kafka.apache.org/downloads 2.下载配置zookeeper http://www.cnblogs.com/eggplantpro/ ...
- 用es实现模糊搜索
Haystack为Django提供了模块化的搜索.它的特点是统一的,熟悉的API,可以让你在不修改代码的情况下使用不同的搜索后端(比如 Solr, Elasticsearch, Whoosh, Xap ...
- 博客系统的使用(typecho、WordPress等等)
一.下载,解压,安装 二.Apache配置虚拟主机,(host文件修改) 三.开启php.ini中pdo类型的扩展适配数据库 四.按照指示页面配置 五.操作控制面板和blog前台
- 使用pytesseract进行图像识别
引言 对于简单验证码及一些图像的识别,我们需要使用pytesseract及相应的Tesseract引擎,它是开源的OCR引擎.帮助我们做一些简单的图像识别 当然为了更好将图片识别,对一些像素比较低的图 ...
- c++和cuda混合编程 实现传统神经网络
直接放代码了... 实现的是x1+x2=y的预测,但梯度下降很慢...233333,gpu运行时间很快!! // // main.cpp // bp // // Created by jzc on 2 ...
- Matlab中矩阵的数据结构
在Matlab中,矩阵默认的数据类型是double, 并不是integer. 而且奇怪的是,矩阵乘法默认按照浮点数类型进行, 整数矩阵相乘会报错.另外,可以用a= int16(A)这种形式实现数据类型 ...
- TCP的几个知识点
1. 三次握手.四次挥手 详细查看:https://www.cnblogs.com/amiezhang/p/6703390.html 2. ARQ 协议 ARQ 就是超时重传机制,分为 2 种:停止等 ...
- C 库函数 - strcspn()
定义 size_t strcspn(const char *str1, const char *str2) 参数 str1 -- 要被检索的 C 字符串. str2 -- 该字符串包含了要在 str1 ...
- rocketMq和kafka的架构区别
概述 其实一直想写一篇rocketMq和kafka在架构设计上的差别,但是一直有个问题没搞明白所以迟迟没动手,今天无意中听人点播了一下似乎明白了这个问题,所以就有了这篇对比. 这篇博文主要讲清楚kaf ...