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, ...
随机推荐
- shell-008:检测502
检测502的方法有多种 1.curl他的状态码(不建议,会对网站造成不必要的访问和多余的日志输出) 2.可以直接检测访问日志 下面用while做成一个死循环监控日志502的状态 #!/bin/bash ...
- Python使用浏览器模拟访问页面之使用ip代理
最近需要使用浏览器模拟访问页面,同时需要使用不同的ip访问,这个时候就考虑到在使用浏览器的同时加上ip代理. 本篇工作环境为win10,python3.6. Chorme 使用Chrome浏览器模拟访 ...
- json对象和数组
Json数据就是格式化的字符串.c#中如果做为参数调用,类型就是string.1.Json数组 方括号[{ "firstName":"John" , " ...
- 在linux上安装 sql server for linux
在linux上安装 sql server for linux Install SQL Server on Red Hat Enterprise Linux Install SQL Server To ...
- javascript中Function与Object
1. 先来一段代码: console.log(Function); // function Function() { [native code] } console.log(Object); // f ...
- Python案例之QQ空间自动登录程序实现
不多说,直接上干货! 工具选择: 电脑系统:win7,32 位,下面第二部安装SetupTools时注意系统版本要求: Python: 2.7.11, 相信只要是2.7的就可以实现: Seleniu ...
- java中为什么重写equals时必须重写hashCode方法?
在上一篇博文Java中equals和==的区别中介绍了Object类的equals方法,并且也介绍了我们可在重写equals方法,本章我们来说一下为什么重写equals方法的时候也要重写hashCod ...
- linux mint 18.2 install erlang
由于Linux min 18.2仓库中的erlnag是18.2,我们手动安装最新版本,参考 https://www.erlang-solutions.com/resources/download.ht ...
- java的文件操作类File
java.io.File类,是java获取文件/文件夹的所有属性,和完成所有相关操作的类 例子: package test.file.IO; import java.io.*; public clas ...
- [中英对照]How PCI Express Works | PCIe工作原理
How PCI Express Works | PCIe工作原理 PCI Express is a high-speed serial connection that operates more li ...