Check if "([]){}" has balanced brackets.
Ready — edit the code above and click Run.
const s = "([]){}";
const stack = [];
const pairs = { ")": "(", "]": "[", "}": "{" };
for (const ch of s) {
if ("([{".includes(ch)) stack.push(ch);
else if (stack.pop() !== pairs[ch]) { console.log(false); process.exit(0); }
}
console.log(stack.length === 0);
Try solving on your own first, then reveal the official answer.
Stack-based bracket matching—top FAANG interview question.