Short Java solution (5 lines). Greedy algorith. O(t.length) time complexity.


  • 0
    O
        public boolean isSubsequence(String s, String t) {
            if (s.length() > t.length()) return false;
            int i = 0, j = 0;
            while(i < s.length() && j < t.length()) 
                if (s.charAt(i) == t.charAt(j++)) i++;
            return i == s.length() ? true : false;
        }
    

Log in to reply
 

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