Given n pairs of parentheses, generate all combinations of well-formed (valid) parentheses.
This is a recursive backtracking problem. At each step, you can either:
( if you haven't used all n opening brackets) if you have more opening than closing (maintains validity)The key insight: at any point, close_count < open_count ensures validity. This is a preview of the backtracking pattern!
generate_parenthesis(2)
['(())', '()()']
generate_parenthesis(3)
['((()))', '(()())', '(())()', '()(())', '()()()']