Search⌘ K
AI Features

Generate all Combinations of Balanced Parentheses

Explore how to generate all valid combinations of balanced parentheses pairs through backtracking. This lesson guides you in writing a recursive function that tracks opening and closing counts to ensure valid sequences. Understand the approach, follow a step-by-step example, and review time and space complexity considerations.

Statement

Write a function to generate all balanced combinations of nn pairs of parentheses.

Example

...
foo a1 N: 1 {} a2 N: 2 {{}} {}{} a3 N: 3 {{{}}} {}{{}} {{}{}} {{}}{} {}{}{}

The test cases will use values of ...