Is Subsequence

题目链接:https://leetcode.com/problems...

greedy, 只要s里面当前的字符可以和t里的字符匹配,s的index就+1

public class Solution {
    public boolean isSubsequence(String s, String t) {
        // 2 points, greedy
        int i = 0, j = 0;
        
        while(i < s.length() && j < t.length()) {
            if(s.charAt(i) == t.charAt(j)) i++;
            j++;
        }
        
        return i == s.length();
    }
}

follow up: string很长,用字典存起来
dict很大,用trie


lulouch13
13 声望6 粉丝