Java solution with two pointers


  • 0
    H
    public boolean isSubsequence(String s, String t) {
    	int m = s.length();
    	int n = t.length();
    	int k = 0;
    	for (int i = 0; i < n && k < m; ++i) {
    		if (s.charAt(k) == t.charAt(i)) {
    			++k;
    		}
    	}
    	return k == m;
    }

Log in to reply
 

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