#20

Valid Parentheses

Easy Stack matching

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether a string of ()[]{} brackets is balanced: every opener is closed by the same type, in the right order.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: does every bracket close in the right order?

Why this works: the most recent unclosed bracket must close first — that's a stack.

Example: "([]{})"

push ) ] → pop ] ✓ → push } → pop } ✓ → pop ) ✓stack empty → valid

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting to check that the stack is empty at the end ("((" is not valid).

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(n).
Topic: Stack & Queue
🎬 Episodes 46–52: video coming soon