Practice

Valid Parentheses

Module 8 · Stacks

Problem

Given a string of only ()[]{}, return whether it is valid: every opener closed by the matching closer type, in the correct nesting order.

Examples

Example 1

Input"()"Outputtrue

Example 2

Input"()[]{}"Outputtrue

Example 3

Input"(]"Outputfalse

Example 4

Input"([)]"Outputfalse

Example 5

Input"([{}])"Outputtrue

Example 6

Input"("Outputfalse

Constraints

1 ≤ n ≤ 10⁴ · only the six bracket characters.

Attempt it first

The matching lesson gave you the algorithm in prose and the three failure modes by name. Your job is a clean translation — and hitting all three failure modes with your own test inputs before running anything.