前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode392. Is Subsequence

leetcode392. Is Subsequence

作者头像
眯眯眼的猫头鹰
发布2019-03-13 16:35:13
2790
发布2019-03-13 16:35:13
举报

题目要求

代码语言:javascript
复制
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的一个子序列。子序列是指s中的字母均按照相对位置存在于t中,比如"abc"是"ahbfdc"的一个子序列,但是"axc"就不是"ahbgdc"的一个子序列。

思路一:java API

java中提供了一个String.indexOf(char c, int startIndex)的方法,这个方法是指从字符串中的startIndex位置开始往后找,返回第一个c所在的下标,如果找不到,则返回-1。利用这个方法我们可以快速的解决这个问题。

代码语言:javascript
复制
    public boolean isSubsequence(String s, String t) {
        if(s==null || s.length()==0) return true;
        int start = -1;
        for(int i = 0 ; i<s.length() ; i++) {
            char c = s.charAt(i);
            //从上一个字符的起始位置开始,找下一个字符第一次出现的下标。如果不存在该字符,则说明不是子序列
            start = t.indexOf(c, start+1);
            if(start < 0) return false;
        }
        return true;
    }

思路二:二分法

二分法的思路主要是指,首先我们遍历字符串t,找到每个字符在t中出现的位置。当我们知道每个字符在t中出现的所有下标后,就开始遍历s,并开始找到距离上一个字符所在的位置之后的当前字符的最小下标。 举例:

代码语言:javascript
复制
s="abc"
t="acbgbc"
遍历t之后可以得到这样一个字段:
a:{0}
b:{2,4}
g:{3}
c:{1,5}

之后遍历s,并用一个index来记录当前字符所在的下标,index初始时为-1。
s[0] = a, a:{0} -> index = 0
s[1] = b, b:{2,4} -> index = 2
s[2] = c, c:{1,5} -> index=5

可以看到我们能够找到一个合法的序列,使得当前字母的起始下标始终大于上一个字母的下标。

代码语言:javascript
复制
    public boolean isSubsequence(String s, String t) {
        List<Integer>[] idx = new List[256]; // Just for clarity
        for (int i = 0; i < t.length(); i++) {
            if (idx[t.charAt(i)] == null)
                idx[t.charAt(i)] = new ArrayList<>();
            idx[t.charAt(i)].add(i);
        }
        
        int prev = 0;
        for (int i = 0; i < s.length(); i++) {
            if (idx[s.charAt(i)] == null) return false; // Note: char of S does NOT exist in T causing NPE
            int j = Collections.binarySearch(idx[s.charAt(i)], prev);
            if (j < 0) j = -j - 1;
            if (j == idx[s.charAt(i)].size()) return false;
            prev = idx[s.charAt(i)].get(j) + 1;
        }
        return true;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目要求
  • 思路一:java API
  • 思路二:二分法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档