Balanced parentheses

ID: balanced-parentheses

A string of opening and closing parentheses is balanced when its total numbers of openings and closings agree and every prefix has at least as many openings as closings. Thus (()()) is balanced, while ())(() has equal totals but an invalid prefix. Reading ( as an up step and ) as a down step gives a bijection with Dyck paths: height is the number of openings awaiting a closing parenthesis. There are balanced strings with pairs, including one empty string. This models nested syntax and the stack discipline that remembers the most recently opened unfinished part.

New to topics? Read the docs here!