/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
const stack = [];
const compareHelper = (a, b) => {
if (a === "(" && b === ")") return true;
if (a === "[" && b === "]") return true;
if (a === "{" && b === "}") return true;
return false;
};
for (const char of s) {
if (char === "(" || char === "{" || char === "[") stack.push(char);
else {
if (!stack.length) return false;
const compareChar = stack.pop();
if (!compareHelper(compareChar, char)) return false;
}
}
return !stack.length;
};
Leave a comment