Simple Java Solution with Two Pointer


  • 0
    Z
    class Solution {
        public boolean isSubsequence(String s, String t) {
            
            if (s.length() > t.length()) return false;
            if (s.length() == t.length()) return s.equals(t);
            
            int i = 0, j = 0;
            while (i < s.length()) {
                while (j < t.length()) {
                    if (s.charAt(i) == t.charAt(j)) {
                        j ++;
                        break;
                    }
                    j ++;
                }
                i ++;
                if (j == t.length()) break;
            }
    
            return i == s.length();
        }
    }
    

Log in to reply
 

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