balanced brackets
The classic stack exercise of checking that every opening bracket in a string has a matching closing bracket in the right order.
In more detail
Push each opening bracket; on a closing bracket, pop and check that it matches. The string is balanced if no check fails and the stack ends empty. Compilers and editors use the same idea to match nested code blocks.
Related terms
- stack — A collection where the last item added is the first one removed, like a pile of plates.
- LIFO — Last in, first out: the ordering rule of a stack, where the newest item is always the next to leave.
Taught in
- Stacks And Queues: Last In Or First In — Algorithms & Data Structures for Working Engineers