392. Is Subsequence - Medium

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 sand 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?

M1: two pointers

在t的长度范围内,比较两个指针指向的字母是否相同。如果相同,移动s指针,如果s到末尾了,返回true,无论是否相同,每次循环都移动t指针。如果t也走到末尾了,返回false

time: O(n), space: O(1)

class Solution {
    public boolean isSubsequence(String s, String t) {
        if(s.length() == 0) {
            return true;
        }
        int i = 0, j = 0;
        while(j < t.length()) {
            if(s.charAt(i) == t.charAt(j)) {
                i++;
                if(i == s.length()) {
                    return true;
                }
            }
            j++;
        }
        return false;
    }
}

M2: follow-up: binary search

需要对大量的s来check t,preprocess t 并存下信息比较合理。可以用hashmap存,遍历t并把每个字母作为key存入hashmap,value是字母出现的索引(list)。然后遍历s,对于s中的每个字母,先取出map中对应的下标list,再用binary search查找 是否存在大于当前字母前一个字母的下标(prev)的 当前字母的出现位置,如果不存在直接返回false,如果存在,prev自增1,直到遍历完s

time: O(MKlogN)  -- M: average length of s, K: number of s, N: length of t (assuming all chars in t are the same)

space: O(length of t)

class Solution {
    public boolean isSubsequence(String s, String t) {
        Map<Character, List<Integer>> map = new HashMap<>();
        for(int i = 0; i < t.length(); i++) {
            map.putIfAbsent(t.charAt(i), new ArrayList<>());
            map.get(t.charAt(i)).add(i);
        }

        int prev = -1;
        for(int i = 0; i < s.length(); i++) {
            List<Integer> list = map.get(s.charAt(i));
            if(list == null) {
                return false;
            } else {
                prev = binarySearch(list, prev);
                if(prev == -1) {
                    return false;
                }
                prev++;
            }
        }
        return true;
    }

    public int binarySearch(List<Integer> list, int target) {
        int left = 0, right = list.size() - 1;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if(list.get(mid) < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left == list.size() ? -1 : list.get(left);
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10245867.html

时间: 2024-11-08 08:08:30

392. Is Subsequence - Medium的相关文章

[LeetCode] 392. Is Subsequence Java

题目: 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 sis a short string (<=100). A subsequence

(Java) 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 sis a short string (<=100). A subsequence of a

[LC] 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

392. Is Subsequence

https://leetcode.com/problems/is-subsequence/#/solutions http://www.cnblogs.com/EdwardLiu/p/6116896.html public boolean isSubsequence(String s, String t) { int j = 0, i = 0; while (j < t.length() && i < s.length()) { if (s.charAt(i) == t.cha

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode题解——算法思想之贪心思想

1. 分配饼干 2. 不重叠的区间个数 3. 投飞镖刺破气球 4. 根据身高和序号重组队列 5. 买卖股票最大的收益 6. 买卖股票的最大收益 II 7. 种植花朵 8. 判断是否为子序列 9. 修改一个数成为非递减数组 10. 子数组最大的和 11. 分隔字符串使同种字符出现在一起 保证每次操作都是局部最优的,并且最后得到的结果是全局最优的. 1. 分配饼干 455. Assign Cookies (Easy) Input: [1,2], [1,2,3] Output: 2 Explanati

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

leetcode-22-string

521. Longest Uncommon Subsequence I find the longest uncommon subsequence of this group of two strings 解题思路: 因为求的是最长uncommon subsequence的长度,所以,如果ab长度不等,应返回长度较大值:ab相同,则返回-1:否则返回长度即可. int findLUSlength(string a, string b) { if (a == b) return -1; if (a