LeetCode 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?
【题目分析】
判断一个字符串是否是另一个字符串的子序列,如果s是t的子序列,t可以这样构成:在s字符串中的任意位置插入任意长度的字符串。
【思路】
1.维持两个字符串指针,分别指向s和t,如果当前字符相同,则指针都向后移动,否则只移动t的指针,直到s中出现的字符都在t中出现过了,我们可以判定s是t的子序列。代码如下:
public class Solution {
public boolean isSubsequence(String s, String t) {
int sindex = 0, tindex = 0;
while(sindex < s.length() && tindex < t.length()) {
if(s.charAt(sindex) == t.charAt(tindex)) {
sindex++;
}
tindex++;
}
if(sindex == s.length()) return true;
return false;
}
}

2.我们对上面的代码进行改进,代码如下:
public class Solution {
public boolean isSubsequence(String s, String t) {
if(t.length() < s.length()) return false;
int prev = 0;
for(int i = 0; i < s.length();i++) {
char tempChar = s.charAt(i);
prev = t.indexOf(tempChar,prev);
if(prev == -1) return false;
prev++;
}
return true;
}
}

可以看到这两种方法的差别很大。之所以有这样的差别,是因为在第一种方法中我们每查看一个字符就要调用一次charAt()方法。而在第二种方法中使用indexOf()方法可以直接跳过不匹配的字符,这样大大减少的了函数的调用次数,减少时间复杂度,简直太棒了!
LeetCode 392. Is Subsequence的更多相关文章
- [leetcode]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 l ...
- [leetcode] 392. Is Subsequence (Medium)
原题 判断子序列 /** * @param {string} s * @param {string} t * @return {boolean} */ var isSubsequence = func ...
- LeetCode 392. Is Subsequence 详解
题目详情 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 & ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- 392. Is Subsequence
392. Is Subsequence 水题,先是判断长度,长度t比s小,返回false,然后从左到右扫描t,然后同时扫描s,如果相同,s的index就往后拉一个,如果s的index等于s长度,返回t ...
- [Leetcode 392]判断子序列 Is Subsequence
[思路] 判断s是否为t的子串,所以length(s)<=length(t).于是两个指针,一次循环. 将s.t转换为数组p1.p2. i为过程中s的匹配长度. i=0空串,单独讨论返回true ...
- [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, ...
- [LeetCode] Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
随机推荐
- Factovisors - PC110704
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10139.html 原创: 作者: ...
- ACE编译运行错误解决
使用VS2010编译运行ACE版本号为6.0.1 按照网上步骤添加config.h,在ACE_wrappers/ace目录下 ,新建 config.h 文件加入以下内容: #include " ...
- JAVA 作业:图形界面
自己动手写的一个小JAVA 程序: 一个学生管理小系统,虽然很挫,但是这我学JAVA的第一步.学了2天JAVA没有白费! import java.awt.*; import java.awt.even ...
- EF POWER TOOLS由数据库逆向CODE FIRST
EF POWER TOOLS由数据库逆向CODE FIRST 前言 利用db first的开发方式有很多可供选择的方案,一种可以用ado.net实体框架模型,由向导直接生成edmx,并生成数据库上下文 ...
- Word2Vec在中文的应用
google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算term之间的相似度,对term聚类等,该项目也支持phrase的自动识别,以及与t ...
- ELF二进制目标文件详解
以下内容为<<linux内核编程>>笔记 链接程序 找出所有引用的外部模块并链接起来,这些外部模块或函数库一般来自于开发者,操作系统和C运行库. 链接程序取出这些函数库,修订指 ...
- FlexPaper+SWFTool+操作类=在线预览PDF(转)
引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...
- linux网络编程(一)
一.概念介绍 网络程序分为服务端程序和客户端程序.服务端即提供服务的一方,客户端为请求服务的一方.但实际情况是有些程序的客户端.服务器端角色不是这么明显,即互为客户端和服务端. 我们编写网络程序时,一 ...
- 几行实现圆形头像,以及一些常见需求形状自定义ImageView组件
在实际开发中,我们经常会遇到这样的需求,就是无论图片长啥样,我们都要其显示成圆形.圆形加一个边框.矩形加边框,带圆角的矩形等等,我已把自己平常用的组件和demo上传到了github(https://g ...
- touch事件分发
touch事件分发 IOS事件分发 我们知道,如果要一个view(就是view,不是UIControl控件)能够响应事件操作,通常的做法是给该View加上相应的手势,或者重写和touch(当然也可以是 ...