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
"()"OutputtrueExample 2
Input
"()[]{}"OutputtrueExample 3
Input
"(]"OutputfalseExample 4
Input
"([)]"OutputfalseExample 5
Input
"([{}])"OutputtrueExample 6
Input
"("OutputfalseConstraints
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.