leetcode 392. Is Subsequence

257 篇文章 17 订阅

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 tt is potentially a very long (length ~= 500,000) string, and sis 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?

讲道理我不太想得通为什么这一题难度级别是Medium,这明明是一道简单题啊?

package leetcode;

public class Is_Subsequence_392 {

	public boolean isSubsequence(String s, String t) {
		char[] sc=s.toCharArray();
		char[] tc=t.toCharArray();
		int scPointer=0;
		int tcPointer=0;
		while(scPointer<sc.length&&tcPointer<tc.length){
			if(sc[scPointer]==tc[tcPointer]){
				scPointer++;
				tcPointer++;
			}
			else{
				tcPointer++;
			}
		}
		if(scPointer==sc.length){
			return true;
		}
		else{
			return false;
		}
			
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Is_Subsequence_392 i=new Is_Subsequence_392();
		System.out.println(i.isSubsequence("axc", "ahbgdc"));
	}

}
大神想法跟我一样。

另外有大神用了下面的方法:Collections.binarySearch的返回值是搜索键的索引,与indexOf方法差不多。

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;//其实我不太懂这一步在干嘛,为啥会出现j<0
            if (j == idx[s.charAt(i)].size()) return false;
            prev = idx[s.charAt(i)].get(j) + 1;
        }
        return true;
}
下面有对这个方法进行的详细说明:

The prev variable is an index where previous character was picked from the sequence. And for the next character to be picked, you have to select it only after this index is the string 'T'.

For instance, if S = "abcd" and T = "abdced".
The index list mapping looks like,

a -> 0
b -> 1
c -> 3
d -> 2,5
e -> 4

After you pick a, and b, c will be picked, and index is 3. Now if you have to pick d, you can't pick index 2 because c was picked at 3, so you have to binary search for index which comes after 3. So it returns 5.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值