Generate Parentheses
Keep two counters, open and close. Add ( while open < n. Add ) only when close < open, which means an unmatched ( is waiting for it. Then every prefix stays balanced, and every string that reaches length 2n is an answer.
A prefix is valid when close never goes above open and open never goes above n. Before each ), the rule to check is close < open, because only an unmatched ( can take it.
Problem
You are given a whole number n. Write out every string that uses exactly n opening brackets ( and n closing brackets ), and is well-formed.
Well-formed means two things. Reading from the left, the count of ( is never lower than the count of ). And at the end both counts are equal.
Return all such strings. The order does not matter.
An integer n.
A list of every well-formed string that has n pairs of brackets.
There are five ways to arrange three pairs. In each one, no prefix has more ) than (.
With one pair, the only well-formed string is ().
- • 1 ≤ n ≤ 8
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Each answer is a string of length 2n. If every place could be
(or), how many strings would that give? - 2.
Is there a string that can never become well-formed, no matter what comes after it? Look at the start of the string.
- 3.
While I build the string, how do I know how many
(are still waiting for a)? - 4.
Can a string start with
)? What does that do to the count? - 5.
If I only allow choices that keep the string valid, will I ever reach a dead end and have to undo a choice?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Build all 2^(2n) strings of brackets, keep the balanced ones | O(n · 2^(2n)) | O(n) |
| Better | Build only strings with exactly n `(` and n `)`, then check balance | O(n · C(2n, n)) | O(n) |
| Optimal | Build left to right; allow `(` while open < n and `)` only while close < open | O(n · Catalan(n)) | O(n) |
Why does this belong to this pattern?
- “generate all strings”The answer is a list of complete candidates, built by choices. That is a search, not a count.
- “well-formed”A rule on every prefix: a `)` can only close a `(` that is still open.
- “n pairs”The length is fixed at 2n, and each bracket is used exactly n times. Stop when the string is complete.
The rule is close < open, checked before each ). Unlike N-Queens or Sudoku, nothing has to be scanned: two counters are the whole state, so each check costs O(1). What makes it different from its neighbours is that every branch the rule allows leads to an answer, so the search never runs into a dead end and never needs to undo a wasted branch.
Don't make this mistake
The count rule (close < n) belongs to the better approach, where strings are only counted. Used here, it lets a ) come first. On n = 2 it returns 6 strings instead of 2, and four of them are not balanced: ())(, )((), )()( and ))((. The check must compare with open, the number of ( so far.
The '(' that was added is never removed, so later strings carry it. On the dry-run input n = 3 this returns 4 strings instead of 5. ((())) is right, but ((()() appears twice and ((())( is not balanced.
Without the test, ( keeps being added past n. On n = 3 the rest of the rules still hold, so the output has 20 strings, not 5, and the first one is ((((((, which has six ( and no ).
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.