Equal Quantities
Every opening bracket `(`, `{`, `[` must have a corresponding closing bracket `)`, `}`, `]`. Just counting them isn't enough though. `)(` has equal quantities but is totally invalid!
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Before writing code, you must understand what makes an expression of brackets "valid". It's not just about counting!
Every opening bracket `(`, `{`, `[` must have a corresponding closing bracket `)`, `}`, `]`. Just counting them isn't enough though. `)(` has equal quantities but is totally invalid!
A closing bracket must match the most recently opened unclosed bracket. In `([)]`, the `)` is invalid because the most recently opened bracket was `[`, not `(`.
"Most recently opened" is a perfect description of Last-In-First-Out behavior. This makes a Stack the perfect and only optimal data structure to validate nested bracket patterns.