leetcode 392. Analyzing sequence (Java greedy)

https://leetcode-cn.com/problems/is-subsequence/submissions/

 

S and t given sequence, it is determined whether the sequence s t a.

 

1. greedy, two pointers a and i, respectively, and traversing s t, a + on the same face, anyway, I have ++. If s.length and a () are equal, return true.

class Solution {
    public boolean isSubsequence(String s, String t) {
        if(s.length()==0) return true;
        int tLen=t.length();
        int a=0;
        for(int i=0;i<tLen;i++){
            if(s.charAt(a)==t.charAt(i)){
                a++;
            }
            if(a==s.length())                 //此语句要放在循环内部
                return true;
        }
        return false;
    }
}

 

Guess you like

Origin www.cnblogs.com/y1040511302/p/11528328.html