BiruLyu
6/27/2017 - 5:53 PM

392. Is Subsequence(1st).java

class Solution(object):
    def isSubsequence(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if not s:
            return True
        j = 0
        for i in range(len(t)):
            if s[j] == t[i]:
                j += 1
                if j == len(s):
                    return True
        return False
public class Solution {
    public boolean isSubsequence(String s, String t) {
        if (s == null || t == null || s.length() > t.length()) return false;
        int i = 0;
        int j = 0;
        while (true) {
            if (i == s.length()) return true;
            if (j == t.length()) return false;
            if (s.charAt(i) == t.charAt(j)) {
                i++;
                j++;
            } else {
                j++;
            }
        }
    }
}
public class Solution {
    public boolean isSubsequence(String s, String t) {
        if (t.length() < s.length()) {
            return false;
        }
        int prev = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            prev = t.indexOf(ch, prev);
            if (prev == -1) {
                return false;
            }
            prev++;
        }
        return true;
    }
}