알고리즘, 자료구조

[leetcode] 20. Valid Parentheses

Developer Mobssie 2022. 10. 24. 18:56
function isValid(s) {
    let stack =[];
    let obj = {
        '(': ')',
        '[': ']',
        '{': '}'
    }
    for(let i = 0; i < s.length; i++){
        if(s[i] === '(' || s[i] === '{' || s[i] === '['){
            stack.push(s[i]);
        } else {
            let peek = stack.pop();
            if(s[i] !== obj[peek]){
                return false
            }
        }
    }
    return !stack.length;
}

https://leetcode.com/problems/valid-parentheses/submissions/