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 ...
随机推荐
- Restful?
为什么要Restful?为什么不Restful? 本随笔不说为什么要Restful,只说为什么不Restful.首先Http是超文本转移协议而不是控制协议.通常文档中也会使用“资源”来指代超文本. ...
- Mobile页面项目总结
项目过去个把月了,一直没有写些东西总结下,这次借着年后的空隙,将当时记录下来的几个点回顾一下. 今明的布局:position技巧 每当看到类似横向并排布局的时候,总是想起定宽浮动,和下面讲到的列表并排 ...
- 新的IRepository接口+搜索和排序解耦(+基于Castle实现)
新的IRepository接口+搜索和排序解耦(+基于Castle实现) 经过了上篇IRepository和IRepository<T>的讨论[文章地址为:http://www.cnblo ...
- jQuery Validation让验证变得如此容易(一)
一.官网下载jquery,和jquery validation plugin http://jqueryvalidation.org/ 二.引入文件 <script src="js/j ...
- synchronized简介
synchronized简介 Java提供了一种内置的锁机制来支持原子性:同步代码块(Synchronized Block).同步代码块包括两部分:一个作为锁对象的引用,一个作为由这个锁保护的代码块. ...
- Asp.Net MVC 进阶篇:路由匹配 实现博客路径 和文章路径
Asp.Net MVC 进阶篇:路由匹配 实现博客路径 和文章路径 我们要实现 通过路由 匹配出 博客地址 和博客文章地址 例如下面的这两个地址 //http://www.cnblogs.com/ma ...
- 目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的选择
目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的选择 ASP.NET Web API能够根据请求激活目标HttpController ...
- Window Service 计划任务
在ASP.NET中创建计划任务,就我个人而言,用过两种方式,一种是用SQL Server创建,把写好的SSIS包导入SQL Server创建的任务中,设置好时间让它去执行.还有一种就是利用window ...
- .NET对象占内存多少
.NET对象占内存多少 一直有一个小小的疑惑——.NET一个对象或者一个集合占多少内存?有没有很快速的方法获取,而不是简单的估计分析对象大小? 查了MSDN,和一些其他人的分析,得到解决是托管代码对象 ...
- Fedora 19的U盘安装 以及简单配置
一.Fedora19的U盘安装 2013年7月3日,Fedora 19正式版发布.Fedora 19除了GNOME桌面版之外,还提供了KDE定制版.LXDE定制版等,有兴趣的网友可以在其官网上下载试用 ...