码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Is Subsequence

时间:2016-11-30 13:45:09      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:greedy   log   bool   key   integer   sea   collect   bsp   asc   

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

标签:greedy   log   bool   key   integer   sea   collect   bsp   asc   

原文地址:http://www.cnblogs.com/EdwardLiu/p/6116896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!