Why does Valid Parentheses use a stack?
Practice this question from Valid Parentheses - Stack matching Pattern Practice Quiz, with options and an explanation.
Question
Why does Valid Parentheses use a stack?
Option A — Correct answer
Nested brackets close in reverse order
Option B
The string is sorted
Option C
Brackets are counted by frequency only
Option D
The answer needs binary search
Explanation
Nested brackets must close in reverse order, which is last-in-first-out behavior.