Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
n = 3["((()))","(()())","(())()","()(())","()()()"]n = 1["()"]Two branching choices at each step: add '(' or ')'. Constraints open<n and close<open prune invalid paths, ensuring only valid parenthesizations are generated. No post-filtering needed — every leaf is valid. Result count equals Catalan number C(n).
Generating all 2 to the 2n strings and checking each one is wasteful. We can build only valid strings.
Track how many opening and closing brackets are used. You may add an opening bracket while open is less than n. You may add a closing bracket only while close is less than open.
Open while open is less than n, close while close is less than open. The rules prune every invalid string before it is built.
Run the animation above on your own input, or watch the 1:29 video explanation of Generate Parentheses with a dry run and Java code.