Simple Java Solution. Keep two pointers.


  • 0
    S
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            if (s.length() > t.length()) {
                return false;
            }
            int i = 0, j = 0, n = s.length(), m = t.length();
            while(i < n && j < m) {
                char ch = s.charAt(i);
                while(j < m && ch != t.charAt(j)) {
                    j++;
                }
                i++;
                j++;
            }
            return i == n;
        }
    }
    

Log in to reply
 

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