Java O(n) solution


  • 0
    D
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            if (s == null || s.length() == 0) {
                return true;
            }
            if (t == null || t.length() == 0) {
                return false;
            }
            for (int i = 0, j = 0; i < s.length(); i++) {
                for (; j < t.length() && s.charAt(i) != t.charAt(j); j++) {}
                if (j == t.length()) {
                    return false;
                } else {
                    j++;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.