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

Taught in

References