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 ...
随机推荐
- ASP.NET MVC 缓存扩展 - Donut Caching
项目介绍 ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code ...
- 第一章 CLR 的执行模型
CLR via C# 读书笔记:第一章 CLR 的执行模型(1) 第Ⅰ部分CLR基础.这部分为三章(第一章:CLR的只想能够模型,第二章:生成.打包.部署和管理应用程序及类型,第三章:共享程序集和强命 ...
- 企业架构研究总结(44)——企业架构与建模之Archimate视图和视角
3. ArchiMate的视角与视图 创建.维护一个企业架构是一件非常复杂繁琐的事情,因为这项工作需要面对许多背景.利益各异的干系人,对他们所关注的问题进行解答,并能够在他们之间形成无障碍的沟通流.为 ...
- .NET 微信开放平台接口(接收短信、发送短信)
.NET 微信开放平台接口(接收短信.发送短信) 前两天做个项目用到了微信api功能.项目完成后经过整理封装如下微信操作类. 以下功能的实现需要开发者已有微信的公众平台账号,并且开发模式已开启.接口配 ...
- 记一次有趣的互联网事件及console.log~
寂寞的中国互联网又一次瘫痪了. 说是顶级域的根挂了,不知道是黑客还是某个实习生干挂的. 反正到现在还没有人来解释这件事. 先普及一下,为什么顶级域的根挂了全中国都挂了. 那是因为dns解析的特点是递归 ...
- 可编辑DIV (contenteditable="true") 在鼠标光标处插入图片或者文字
近期需开发一个DIV做的编辑器,插入表情图片可直接预览效果,仔细参考了下百度贴吧的过滤粘贴过来文件的THML代码,自己整理了下.写出来只是和大家分享下,我自己也不大懂,经过努力,幸好搞定. 蛋疼的事情 ...
- NSSortDescriptor(数组排序)
如果数组里面的每一个元素都是一个个model,例如 DepartsDate.h文件 [plain] view plaincopy #import <Foundation/Foundation.h ...
- 运用Unity实现AOP拦截器
运用Unity实现AOP拦截器[结合异常记录实例] 本篇文章将通过Unity实现Aop异常记录功能:有关Unity依赖注入可以看前两篇文章: 1:运用Unity实现依赖注入[结合简单三层实例] 2:运 ...
- 调WScript.Shell时报错:Automation 服务器不能创建对象
我们经常需要通过生成ActiveXObject("WScript.Shell");来调某一exe文件, 如 //设置网页打印的页眉页脚为空 var HKEY_Root,HKEY_P ...
- json-smart 使用示例
json-smart 使用示例 json是一种通用的数据格式.相比与protocal buffer.thrift等数据格式,json具有可读性强(文本).天生具备良好的扩展性(随意增减字段)等优良特点 ...