2 Pointers Using Java


  • 0
    A
    public class Solution {
        public boolean isSubsequence(String s, String t) {
            
            if(t.length()<s.length())
                return false;
            if(s.length()<1)
                return true;
            if(t.length()<1)
                return false;
                
            int tIndex=0;
            int sIndex=0;
            
            while(tIndex<t.length())
            {
                while(tIndex<t.length() && t.charAt(tIndex)!=s.charAt(sIndex))
                    tIndex++;
                
                if(tIndex>=t.length())
                    break;
                
                tIndex++;
                sIndex++;
                
                if(sIndex>=s.length())
                    return true;
               
            }
            
            return false;
        }
    }
    

Log in to reply
 

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