DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 392. Is Subsequence

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; 
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)