Two Pointer Solution


  • 0
    B
      public boolean isSubsequence(String s, String t) {
                 if (t.length() < s.length()) return false;
            if (s.length() == t.length()) return s.equals(t);
            if (s.length() == 0) return true;
            int index1 = 0,index2 = 0;
            while (index1<s.length()&&index2<t.length()){
                if (s.charAt(index1)==t.charAt(index2)){
                    index1++;
                }
                index2++;
            }
            return index1==s.length();
        }

Log in to reply
 

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