leetcode 392

判断子序列

Posted by Static on July 27, 2020

题目链接:https://leetcode-cn.com/problems/is-subsequence/

题目描述


分析

1. 遍历


代码实现

public enum Q392 {
    instance;

    public boolean isSubsequence(String s, String t) {
        if(s==null||s.length()==0)return true;
        int i=0,j=0;
        for(;i<s.length();){
            for(;j<t.length();j++){
                if(t.charAt(j)==s.charAt(i)){
                    ++i;
                    ++j;
                    break;
                }
            }
            if(j==t.length()){
                return i == s.length();
            }
            if(i == s.length()){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        // assert true
        System.out.println(Q392.instance.isSubsequence("abc","ahbgdc"));

        // assert false
        System.out.println(Q392.instance.isSubsequence("axc","ahbgdc"));

        // assert false
        System.out.println(Q392.instance.isSubsequence("aaaaaa","bbaaaa"));
    }
}

Everyone’s the hero in their own story.

每个人都是自己故事里的英雄。