DEV Community

tamilvanan
tamilvanan

Posted on

1

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)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay