Open this example in the interactive library
Source code
const valid = (s) => {
const stack = [],
pairs = { ")": "(", "]": "[", "}": "{" };
for (const c of s) {
if ("([{".includes(c)) stack.push(c);
else if (c in pairs && stack.pop() !== pairs[c]) return false;
}
return stack.length === 0;
};
console.log(valid("([]){}"), valid("([)]"));
Sample output
true false
