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, ...
随机推荐
- 原 form 表单中 disabled 属性的元素不参与表单提交
https://blog.csdn.net/benben683280/article/details/79173336
- python连接,操作 InfluxDB
准备工作 启动服务器 执行如下命令: service influxdb start 示例如下: [root@localhost ~]# service influxdb start Starting ...
- Go语言管道
Channel概念 Channel 是Go中的一个核心类型,你可以把它看成一个管道.Channel是引用类型,操作符是箭头 <- . Channel 是 CSP 模式的具体实现,用于多个 gor ...
- cool kickass
I can stay like this alllllllllll daaaaaaaaayyyyyy.
- [Re:从零开始的分布式] 0.x——分布式基础概念
分布式的特点 1. 分布式 2. 对等性 3. 并发性 4. 缺乏全局时钟 5. 故障总是会发生 分布式环境的问题 1. 网络不可靠 2. 网络分区 3. 节点故障 CAP理论 一致性 可用性 分区容 ...
- MySQL自定义排序
存在表A 按名字倒序排 SELECT * FROM A ORDER BY name DESC 结果如下: 若需要按照王五.张三.李四的顺序排序,使用自定义排序:FIELD() SELEC ...
- [转] Java中Comparator进行对象排序
[From] https://blog.51cto.com/thinklili/2063244 Java在8后引入了lambda表达式和流,使得排序方法有了变化 class User { int id ...
- golang (5) http 请求分析
http 分析包分析 fmt.Println("get Content-Type: ", r.Header.Get("Content-Type")) var r ...
- (转)漫谈JVM
漫谈JVM 原文:https://liuzhengyang.github.io/2016/10/05/gossip-jvm/ 背景介绍 JVM已经是Java开发的必备技能了,JVM相当于Java的操作 ...
- struts2上传单个文件
项目目录: struts.xml配置: <constant name="struts.enable.DynamicMethodInvocation" value=" ...