shlogg · Early preview
Rakesh Reddy Peddamallu @rakesh678219

Checking Subsequence In Strings With IsSubsequence Function

Check every char in s is present in t sequentially. If found, skip to next char in t. This code implements this idea.

Since we need to check every character of s to be sequentially present in t , if we find the character  of s in t , then we can find the next char of s in the subsequent part of t ie we now don't need to go through every char of t . this is the idea behind the given code

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isSubsequence = function(s, t) {
    for(c of s){
        let index = t.indexOf(c);
        if(index===-1){
            return false;
        }
        t = t.substring(index+1);
    }
    return true; 
};