my elegant java solution - two pointer


  • 0
    O
     public boolean isSubsequence(String s, String t) {
        	int is = 0, it = 0; //two pointer
        	
        	while(it < t.length() && is < s.length()){
        		char cs = s.charAt(is);
        		char ct = t.charAt(it);
        		if(ct == cs){
        			++ is; ++ it;
        		}else ++ it;
        	}
        	return is == s.length();
        }
    

Log in to reply
 

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