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.

算法分析

该问题的算法还是很简单的,直接从s和t的首端开始遍历比较字符是否相等即可,如果不等,则增加在t中的下标位置;如果相等,则同时增加在s和t中的下标位置。如果t中的指标位置增长到了t的末尾,而s中的指标还没有增长的末尾,则返回false。如果s中的指标先增长到了末尾,则返回true。

Java算法实现

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

LeetCode赛题392---- Is Subsequence的更多相关文章

  1. LeetCode算法题-Longest Harmonious Subsequence(Java实现)

    这是悦乐书的第270次更新,第284篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是594).我们定义一个和谐数组是一个数组,其最大值和最小值之间的差 ...

  2. LeetCode算法题-Longest Uncommon Subsequence I(Java实现)

    这是悦乐书的第252次更新,第265篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第119题(顺位题号是521).给定一组两个字符串,您需要找到这组两个字符串中最长的不同 ...

  3. LeetCode赛题515----Find Largest Element in Each Row

    问题描述 You need to find the largest element in each row of a Binary Tree. Example: Input: 1 / \ 2 3 / ...

  4. LeetCode赛题----Find Left Most Element

    问题描述 Given a binary tree, find the left most element in the last row of the tree. Example 1: Input: ...

  5. LeetCode赛题395----Longest Substring with At Least K Repeating Characters

    395. Longest Substring with At least K Repeating Characters Find the length of the longest substring ...

  6. LeetCode赛题394----Decode String

    394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...

  7. LeetCode赛题393----UTF-8 Validation

    393. UTF-8 Validation A character in UTF8 can be from 1 to 4 bytes long, subjected to the following ...

  8. LeetCode赛题391----Perfect Rectangle

    #391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...

  9. LeetCode赛题390----Elimination Game

    # 390. Elimination Game There is a list of sorted integers from 1 to n. Starting from left to right, ...

随机推荐

  1. mariadb配置主从同步遇到的问题

    一:ERROR: No query specified 解决方案: \G后面不能再加分号;,因为\G在功能上等同于;,如果加了分号,那么就是;;(2个分号),SQL语法错误 二:主从同步不成功 Sla ...

  2. SSH使用密钥免密码登录

    使用ssh远程连接服务器,有两种身份校验方式:账号密码和秘钥.使用秘钥的方式理论上更加安全,而且免去了输入密码的步骤,使用起来更方便(尤其对于sftp,scp等). 设置 SSH,打开密钥登录功能 编 ...

  3. 【Maven学习】maven基本命令

    maven最主要的命令如下: mvn clean compile:告诉Maven编译项目主代码 mvn clean test:执行src/test/main下面的test方法,在执行测试之前,会自动执 ...

  4. 【es6】正则扩展

  5. eclipse自定义代码注释

    效果如下: 自定义注释的方法:

  6. Maven项目中Spring整合Mybatis

    Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...

  7. CPU缓存一致性协议与java中的volatile关键字

    有关缓存一致性协议MESI自行百度. 提出问题:volatile在缓存一致性协议上又做了哪些事情?为啥它不保证原子性? 在缓存一致性协议下,CPU为了执行效率使用了写(存储)缓存和失效队列从而导致对用 ...

  8. Charles 抓取 iphone https的设置方式

    1. Charles:  help > SSL Proxying > Install Charles Root Certificate, 2. 将会打开 钥匙串访问 的功能,查找 Char ...

  9. Kernel的IIC驱动分析

    涉及到的文件: drivers/i2c/i2c-core.c drivers/i2c/i2c-dev.c drivers/i2c/busses/i2c-imx.c 等等 在下面分析的代码中,不想关或者 ...

  10. 帧布局--FrameLayout

    <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android=" ...