harrytothemoon / leetcodeAplus

Leetcode meeting note
2 stars 0 forks source link

[20] Valid Parentheses #89

Open tsungtingdu opened 3 years ago

tsungtingdu commented 3 years ago
var isValid = function(s) {
    let sets = {
        '(' : ')',
        '{' : '}',
        '[' : ']'
    }

    let stack = []

    for (let i = 0; i < s.length; i++) {
        if (s[i] in sets) {
            stack.push(s[i])      // find left 
        } else {
            let temp = stack.pop()
            if (sets[temp] !== s[i]) return false
        }
    } 
    return stack.length === 0
};