WorkspaceProblem lab
ALGORITHM VISUALIZER

Valid Parentheses

Opening the interactive workspace…

Understanding Valid Parentheses

Given a string containing only parentheses, square brackets, and braces, determine whether every closing bracket matches the most recent unmatched opening bracket.

How it works

The next closing bracket must match the last opening bracket that has not been closed.

Push opening brackets. For every closing bracket, pop and compare. A mismatch or underflow fails immediately; the stack must be empty at the end.

Try an example

Input: s = "([]){}"

Output: true

Every closing bracket matches in last-in-first-out order.

Input: s = "([)]"

Output: false

The closing parenthesis arrives before the square bracket is closed.

Constraints

  • Allowed characters: ()[]{}.
  • The empty string is valid.
  • Visualization limit: 64 brackets.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity