Skip to main content

20.js

/** ------------------------------------------------------------------------------
*
* 2023-04-10
* Valid Parentheses
* https://leetcode.com/problems/valid-parentheses/
*
------------------------------------------------------------------------------ */
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
const stack = [];

for (const p of s) {
if (p === ")") {
if (stack[stack.length - 1] !== "(") return false;
stack.pop();
} else if (p === "}") {
if (stack[stack.length - 1] !== "{") return false;
stack.pop();
} else if (p === "]") {
if (stack[stack.length - 1] !== "[") return false;
stack.pop();
} else {
stack.push(p);
}
}

return stack.length === 0;
};