Straight forward Javascript solution with O(n)


  • 0
    S

    Just keep get substring of String t and find if each char in String s existing in String t leftover.

    /**
     * @param {string} s
     * @param {string} t
     * @return {boolean}
     */
    var isSubsequence = function(s, t) {
        
        if(s.length === 0){
            return true;
        }
        
        if(s.length !== 0 && t.length === 0){
            return false;
        }
        
        var dp = [];
        
        for(var i = 0; i < s.length; i++){
            var temp = t.indexOf(s[i]);
            if(temp < 0){
                return false;
            } else {
                dp.push(dp[dp[dp.length - 1]] + temp);
                t = t.substr(temp+1);
            }
        }
        
        return true;
    };
    

Log in to reply
 

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