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.
算法分析
该问题的算法还是很简单的,直接从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的更多相关文章
- LeetCode算法题-Longest Harmonious Subsequence(Java实现)
这是悦乐书的第270次更新,第284篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是594).我们定义一个和谐数组是一个数组,其最大值和最小值之间的差 ...
- LeetCode算法题-Longest Uncommon Subsequence I(Java实现)
这是悦乐书的第252次更新,第265篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第119题(顺位题号是521).给定一组两个字符串,您需要找到这组两个字符串中最长的不同 ...
- 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 / ...
- LeetCode赛题----Find Left Most Element
问题描述 Given a binary tree, find the left most element in the last row of the tree. Example 1: Input: ...
- 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 ...
- LeetCode赛题394----Decode String
394. Decode String Given an encoded string, return it's decoded string. The encoding rule is: k[enco ...
- 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 ...
- LeetCode赛题391----Perfect Rectangle
#391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...
- LeetCode赛题390----Elimination Game
# 390. Elimination Game There is a list of sorted integers from 1 to n. Starting from left to right, ...
随机推荐
- CountDownLatch、CyclicBarrier和 Semaphore
在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法. 以下 ...
- abp 依赖注入声明
public class SchedulerManager : ISingletonDependency { private ILogger logger; public SchedulerManag ...
- Mac 10.12安装WebStorm
下载: (链接: https://pan.baidu.com/s/1c2o8wUG 密码: 2waz)
- Mac下使用Wine安装正则表达式工具RegexBuddy 4
下载: (链接: https://pan.baidu.com/s/1bzRae6 密码: b5d3) 安装: 1.安装Wine 参考:http://www.cnblogs.com/EasonJim/p ...
- Sublime Text 3新建工程
1. 创建工程 Project > Add Folder to Project 这时在sidebar中将出现刚刚添加的文件目录,如果还需要添加其他目录,则重复这一操作即可. 2. 保存工程 Pr ...
- (转)Python3 日期和时间
Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. ...
- (转)MySql数据库4【命令行赋权操作】
MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 原文:http://www.cnblogs.com/zhuyibo/p/3980328.html 一.g ...
- java_Proxy动态代理_AOP
讲了JDK里使用Proxy动态代理的机制,详细过程. 切面类TransactionHandler需要实现InvocationHaandler接口,实现它的invoke方法. 项目目录: User类代码 ...
- window下eclipse安装python插件
1.安装python环境 python安装包下载地址:https://www.python.org/downloads/windows/ 2.在eclipse中在线安装PyDev插件 启动Eclips ...
- DiagnosticFormatter
关于这个类的继承体系如下: 1.DiagnosticFormatter类在com.sun.tools.javac.api包中,其定义如下: /** * Provides simple function ...