JAVA BEATS 90% TWO POINTERS FOR BOTH STRINGS


  • 0
    M
    public boolean isSubsequence(String s, String t) {
        if(s.length() > t.length()) return false;
        int left=0, right=s.length()-1;
        
        while(left<=right){
            int first = t.indexOf(s.charAt(left));
            int last = t.lastIndexOf(s.charAt(right));
            if(first == -1 || last == -1) return false;
            
            if (left!=right && first>=last)  return false;
            if(left<right) t= t.substring(first+1, last);
            left++; right--;
        }
        
        return true;
    

    }


Log in to reply
 

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