๐Ÿ”’Algorithm

LeetCode [JS] > 392. Is Subsequence

devWarrior 2025. 3. 27. 23:30

โœ…๋งํฌ

https://leetcode.com/problems/is-subsequence/description/

โœ…ํ’€์ด

์‰ฝ๊ฒŒ ๋ฌธ์ œ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n) ์ด๋‹ค. 

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isSubsequence = function(s, t) {
    
    let sIdx = 0, tIdx = 0

    while(sIdx<s.length && tIdx<t.length){
        if(s[sIdx]===t[tIdx]){
            ++sIdx
            ++tIdx
        }else{
            ++tIdx
        }
    }
    return sIdx === s.length ? true:false
};

//  ์‹œ๊ฐ„ ๋ณต์žก๋„  log(t.lenght)