Leetcode: Is Subsequence

Posted neverlandly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode: 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?

Basic Solution: DP, O(mn) time, O(m) space, m is the size of s, n is the size of t

 1 public class Solution {
 2     public boolean isSubsequence(String s, String t) {
 3         if (s.length() == 0) return true;
 4         boolean[] dp = new boolean[s.length()+1];
 5         dp[0] = true;
 6         for (int i=1; i<=t.length(); i++) {
 7             for (int j=s.length(); j>=0; j--) {
 8                 if (dp[j] || t.charAt(i-1)==s.charAt(j-1) && dp[j-1])
 9                     dp[j] = true;
10             }
11             if (dp[s.length()]) return true;
12         }
13         return false;
14     }
15 }

 

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

 1 public class Solution {
 2     public boolean isSubsequence(String s, String t) {
 3         if (s.length() == 0) return true;
 4         int match = 0;
 5         for (int i=0; i<t.length(); i++) {
 6             if (t.charAt(i) == s.charAt(match)) {
 7                 match++;
 8             }
 9             if (match == s.length()) return true;
10         }
11         return false;
12     }
13 }

 Follow Up:

The best solution is to create a map for String t, key is char, value is the index of appearance in ascending order

 1     public boolean isSubsequence(String s, String t) {
 2         List<Integer>[] idx = new List[256]; // Just for clarity
 3         for (int i = 0; i < t.length(); i++) {
 4             if (idx[t.charAt(i)] == null)
 5                 idx[t.charAt(i)] = new ArrayList<>();
 6             idx[t.charAt(i)].add(i);
 7         }
 8         
 9         int prev = 0;
10         for (int i = 0; i < s.length(); i++) {
11             if (idx[s.charAt(i)] == null) return false; // Note: char of S does NOT exist in T causing NPE
12             int j = Collections.binarySearch(idx[s.charAt(i)], prev);
13             if (j < 0) j = -j - 1;
14             if (j == idx[s.charAt(i)].size()) return false;
15             prev = idx[s.charAt(i)].get(j) + 1;
16         }
17         return true;
18     }

 

以上是关于Leetcode: Is Subsequence的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode | 0392. Is Subsequence判断子序列Python

LeetCode --- 392. Is Subsequence 解题报告

Leetcode Is Subsequence

[leetcode-392-Is Subsequence]

LeetCode 392. Is Subsequence

[LeetCode] 392. Is Subsequence Java