When to use it
Bracket validation, parsing nested structures, removing invalid parentheses variants.
Pattern
Use a stack to match the most recent unmatched opener when you see a closer.
Bracket validation, parsing nested structures, removing invalid parentheses variants.
Reference implementation in C#, Python, Java, JavaScript, and C++.
Teaches the stack discipline used in many later problems (next greater, histogram, calculators).
Drill Valid parentheses with Stack & Monotonic Stack problems — filter the bank and solve 2–3 Easy/Medium items while saying the invariant out loud.
Related Stack & Monotonic Stack problems — open any one and apply Valid parentheses.
Valid Parentheses
Stack
Longest Valid Parentheses
Stack / DP
Trapping Rain Water
Two Pointers
Largest Rectangle in Histogram
Monotonic Stack
Maximal Rectangle
Stack / DP
Binary Tree Inorder Traversal
Tree
Min Stack
Design
Decode String
Stack
Valid Parenthesis String
Greedy