资讯专栏INFORMATION COLUMN

leetcode392. Is Subsequence

youkede / 2290人阅读

摘要:题目要求如何判断字符串是否是字符串的一个子序列。子序列是指中的字母均按照相对位置存在于中,比如是的一个子序列,但是就不是的一个子序列。可以看到我们能够找到一个合法的序列,使得当前字母的起始下标始终大于上一个字母的下标。

题目要求
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。利用这个方法我们可以快速的解决这个问题。

    public boolean isSubsequence(String s, String t) {
        if(s==null || s.length()==0) return true;
        int start = -1;
        for(int i = 0 ; i
思路二:二分法

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

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

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

    public boolean isSubsequence(String s, String t) {
        List[] 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;
    }

想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72719.html

相关文章

  • [LeetCode/LintCode] Is Subsequence

    Problem 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) ...

    terasum 评论0 收藏0
  • LeetCode[300] Longest Increasing Subsequence

    摘要:再用二分法找当前值应该在排好序的数组中的插入位置。因为要找的是最长的序列,所以每次将排好序的数组中替换成已经排好序的,会能保证得到的结果是最长的。保证升序相等也要替换这个值 LeetCode[300] Longest Increasing Subsequence Given an unsorted array of integers, find the length of longe...

    blankyao 评论0 收藏0
  • [LeetCode] 300. Longest Increasing Subsequence

    Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest increasing subsequence is [2,3,7...

    luckyyulin 评论0 收藏0
  • [LeetCode] 727. Minimum Window Subsequence

    Problem Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W. If there is no such window in S that covers all characters in T, return the empty string...

    kaka 评论0 收藏0
  • leetcode376. Wiggle Subsequence

    摘要:题目要求扭动序列是指数组中的相邻两个元素的差保证严格的正负交替,如数组中相邻两个元素的差为,满足扭动序列的要求。现在要求从一个数组中,找到长度最长的扭动子序列,并返回其长度。即前一个元素和当前元素构成下降序列,因此代码如下 题目要求 A sequence of numbers is called a wiggle sequence if the differences between ...

    CoffeX 评论0 收藏0

发表评论

0条评论

youkede

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<