Generate Balanced Parentheses

medium
Unit 16 · Backtracking0/3 to milestone

Return all strings of n pairs of balanced parentheses, in lexicographic order ('(' before ')'). Use backtracking: add ( while you have some left, and ) only when it wouldn't close more than you've opened.

The number of results is the n-th Catalan number.

#backtracking#recursion2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Generate Balanced Parentheses · TechGradify