Generate Parentheses

medium

You are given an integer n — the number of pairs of parentheses at your disposal. Build every distinct well-formed string that uses exactly n opening brackets and n closing brackets, and return them all.

A string is well-formed when every ( is closed by a matching ) that comes after it — equivalently, no prefix of the string ever closes more brackets than it has opened, and the whole string ends perfectly balanced.

Return the combinations in any order.

Hints

Could you build the strings one character at a time? At each position there are only two characters you could possibly place.
Not every prefix can grow into a valid string. What must be true about the counts of ( and ) in any prefix that is still worth continuing?
Keep two counters: openers used and closers used. Place ( while openers < n; place ) while closers < openers. Recurse, then undo the choice — every string that reaches length 2n is automatically valid.

Common doubts

The rules maintain closed <= opened <= n at every step, which means the running balance opened - closed never goes negative. When the string reaches length 2n, those inequalities force opened = closed = n — balanced and complete.
At each step the two branches place a different character next, so any two finished strings differ at the first position where their construction paths diverged. Every string is built along exactly one path.
The n-th Catalan number: 1, 2, 5, 14, 42, 132, 429, 1430 for n = 1..8. It grows like 4^n / (n^1.5 · sqrt(pi)) — exponential, but far slower than the 2^(2n) raw candidates.
Not with a single bracket type. All a stack would tell you is its height, and the height is exactly opened - closed — so two integers replace it. With multiple bracket types you would need the real stack.

Interview follow-ups

Precompute how many completions exist from each state (a Catalan-style DP over remaining openers and current balance). At each position, count the completions that start with ( — if k exceeds that, subtract and place ) instead. This walks straight to the k-th string in O(n^2).
The stock rule still limits openers, but two counters no longer suffice — a closer must match the most recent unclosed opener, so you carry a real stack of open bracket types in the recursion state.
Yes — either simulate the recursion with an explicit stack of (path, opened, closed) states, or build the set for n from the identity that every balanced string is ( A ) B with A and B balanced — the same decomposition that proves the Catalan count.

Fun facts

  • The number of valid strings is the Catalan number C(n) — the same sequence that counts binary trees with n nodes, mountain ranges drawn with up-strokes and down-strokes, and ways to cut a polygon into triangles.
  • The two counters opened and closed are a compressed stack: with only one bracket type, the only thing that matters about the stack is its height.
  • The push–recurse–pop skeleton in the optimal solution is the exact template behind Subsets, Combination Sum, Palindrome Partitioning, and N-Queens — this problem is the smallest place to learn it.

Asked at

AmazonGoogleMetaMicrosoftUberApple
Frequently Sometimes Occasionally
Example 1
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
These are the only five balanced arrangements of 3 pairs — a string like ()) ( is never produced because it closes a bracket that was never opened.
Example 2
Input: n = 1
Output: ["()"]
One pair can only be arranged one way.
Constraints

- 1 <= n <= 8

Solve this problem →