Simple Java O(n) solution


  • 0
    H
    public boolean isSubsequence(String s, String t) 
    	{	
    		if(s == null)
    			return true;
    		if(t == null)
    			return false;
    		
    		int ls = s.length();
    		int lt = t.length();
    		int is = 0, it = 0;
    		while(is < ls && it < lt)
    		{
    			if(s.charAt(is) == t.charAt(it))
    				is++;
    			it++;
    		}
    		
    		if(is < ls)
    			return false;
    		else
    			return true;
        }
    

Log in to reply
 

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