DEV Community

tamilvanan
tamilvanan

Posted on

20. Valid Parentheses

problem description

my first solution:

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
    let stack = [];
    let compliments = { ")": "(", "]": "[", "}": "{" };

    for (let char of s) {
        if (char === "(" || char === "[" || char === "{") {
            stack.push(char);
        } else if (stack.length === 0 || stack.pop() !== compliments[char]) {

            return false;

        }
    }

    return stack.length === 0;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)