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的更多相关文章

  1. [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 ...

  2. [leetcode] 392. Is Subsequence (Medium)

    原题 判断子序列 /** * @param {string} s * @param {string} t * @return {boolean} */ var isSubsequence = func ...

  3. LeetCode 392. Is Subsequence 详解

    题目详情 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 & ...

  4. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  5. 392. Is Subsequence

    392. Is Subsequence 水题,先是判断长度,长度t比s小,返回false,然后从左到右扫描t,然后同时扫描s,如果相同,s的index就往后拉一个,如果s的index等于s长度,返回t ...

  6. [Leetcode 392]判断子序列 Is Subsequence

    [思路] 判断s是否为t的子串,所以length(s)<=length(t).于是两个指针,一次循环. 将s.t转换为数组p1.p2. i为过程中s的匹配长度. i=0空串,单独讨论返回true ...

  7. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  8. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. [LeetCode] Minimum Window Subsequence 最小窗口序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...

随机推荐

  1. [转]网上看到的关于C中内存分配的描述

    1栈   -   有编译器自动分配释放     2堆   -   一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收     3全局区(静态区),全局变量和静态变量的存储是放在一块的,初始 ...

  2. Windows Forms框架编程

    <Windows Forms框架编程>节选   第九章 设计模式与原则 软件设计模式(Design pattern)是一套被反复使用的代码设计经验总结.使用设计模式是为了可重用代码.让代码 ...

  3. c#中关于大对象数组的一些心得

    在之前的一个课题中,曾经需要用到2W行*3W列的float类型矩阵(大约2.4G),由于无法创建大于2G的对象,当时采用了一些取巧的办法回避了,并没有拿出精力来研究一下这个问题.今天和公司的张哥(大牛 ...

  4. Knockout 可扩展性

    你需要知道的顶级特性 Knockout 最棒的一个特点就是它的可扩展性.Knockout 存在大量的扩展点,包含大量的工具来创建我们的应用程序.许多开发者除了 Knockout 核心库之外没有使用任何 ...

  5. 关于C#时间格式化中的“f”

    示例: DateTime.Now.ToString("yyyyMMddHHmmssfff") 上面的示例就是将日期格式化到毫秒级.那么问题来了,格式化到微秒级.纳秒级怎么整?f又是 ...

  6. linux下编译php追加enable的方法

    如果我们运行php时发现缺少某个库,在windows环境下很简单,找到.dll 对应的库文件,然后拷贝到 extension 目录下,然后在php.ini 里 去掉 前面的分号或者 追加一行 exte ...

  7. 使用Entity Framework 4进行代码优先开发

    [原文地址]Code-First Development with Entity Framework 4   .NET 4随带发布了一个改进版的Entity Framework(EF)- 一个位于Sy ...

  8. 开发者所需要知道的iOS7 SDK新特性

    iOS 7 春风又绿加州岸,物是人非又一年.WWDC 2013 keynote落下帷幕,新的iOS开发旅程也由此开启.在iOS7界面重大变革的背后,开发者们需要知道的又有哪些呢.同去年一样,我会先简单 ...

  9. [置顶] 学习JDK源码:编程习惯和设计模式

    编程习惯 1.用工厂方法替代构造函数 Boolean.valueOf() 通过一个boolean简单类型,构造Boolean对象引用. 优点:无需每次被调用时都创建一个新对象.同时使得类可以严格控制在 ...

  10. jQuery判断浏览器类型

    if ($.browser.msie) { alert("IE浏览器"); } else if ($.browser.opera) { alert("opera浏览器&q ...