Home > Web Front-end > JS Tutorial > body text

Valid Parentheses

Mary-Kate Olsen
Release: 2024-11-24 11:49:12
Original
447 people have browsed it

 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;
};

Copy after login

The above is the detailed content of Valid Parentheses. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template