Hard javascript

Valid parentheses

Problem

Check if "([]){}" has balanced brackets.

Hints
  • Use a stack; push open brackets, pop on close.

Your practice code

Ready — edit the code above and click Run.

Solution

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.

Explanation

Stack-based bracket matching—top FAANG interview question.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details