Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

LeetCode: Is Subsequence (2ms)

 


public class Solution {
    public boolean isSubsequence(String s, String t) {
        int pos = 0;
        int prevPos = 0;
        
        for(int i = 0; i < s.length(); i++) {
            
            if (t.indexOf(s.charAt(i), pos) == -1 || prevPos > t.indexOf(s.charAt(i), pos)) {
                return false;
            }
            
            prevPos = t.indexOf(s.charAt(i), pos);
            pos = prevPos + 1;
        }
        
        return true;
    }
}

comments powered by Disqus