Home
DSA Patterns
Backtracking
Generate Parentheses
Generate Parentheses (LeetCode 22)
Generate Parentheses (LeetCode 22): build only well-formed strings of n pairs, using two guards instead of generating and filtering.
Examples
The five well-formed strings of three pairs: Catalan(3).
One pair, one way to write it.
Problem Understanding
List every string that uses exactly n opening and n closing parentheses and is well formed, meaning no prefix ever closes more brackets than it has opened.
Example: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Algorithm Strategy
Instead of generating all 2²ⁿ strings and validating them, we can build only valid strings.
Rules:
- Open: We can add an open parenthesis
(ifopen_count < n. - Close: We can add a closing parenthesis
)ifclose_count < open_count.
This ensures we never close more than we open (validity) and never exceed n (completeness).
Interactive Visualization
ONE NODE PER CALL · THE LABEL IS THE BRACKET ADDED, BELOW IT: OPENED,CLOSED SO FAR
EMPTY RESULT AND PATH
opened
0
closed
0
Answer
:
path (0 of 6)
"": empty
result
Nothing recorded yet
Start with an empty result and an empty path. 3 pairs, so every answer is 6 characters long
Next
0 opened, fewer than 3: add "(": the path is (
n
Every node is one call to backtrack(open, close). Its label is the bracket that call added, and the pair under it is opened, closed: the two counts the guards test. ( branches go left and ) branches go right.
A call with only one child is a guard that said no, and the figure stops on that check as a step of its own: no ( once all n are open, and no ) when there is nothing open to close. The bottom row is the answer, in the order the code returns it.
Dry Run
Tracing n = 2.
| # | Call | Path | What happens |
|---|---|---|---|
| 1 | backtrack(0, 0) |
"" |
0 < 2: add ( → backtrack(1, 0) |
| 2 | backtrack(1, 0) |
( |
1 < 2: add ( → backtrack(2, 0) |
| 3 | backtrack(2, 0) |
(( |
2 < 2 fails: no (. 0 < 2: add ) → backtrack(2, 1) |
| 4 | backtrack(2, 1) |
(() |
No (. 1 < 2: add ) → backtrack(2, 2) |
| 5 | backtrack(2, 2) |
(()) |
Length 4: record (()), return |
| 6 | backtrack(2, 1), then backtrack(2, 0) |
(( |
Pop ) twice: both return |
| 7 | backtrack(1, 0) |
( |
Pop (. 0 < 1: add ) → backtrack(1, 1) |
| 8 | backtrack(1, 1) |
() |
1 < 2: add ( → backtrack(2, 1) |
| 9 | backtrack(2, 1) |
()( |
No (. 1 < 2: add ) → backtrack(2, 2) |
| 10 | backtrack(2, 2) |
()() |
Length 4: record ()(), return |
| 11 | backtrack(1, 1) |
() |
Pop (. 1 < 1 fails: no ). Return |
| 12 | backtrack(0, 0) |
"" |
Pop (. 0 < 0 fails: no ). Return |
Result: ["(())", "()()"]: every string was valid when it was built, so nothing was generated and thrown away.
Edge Cases & Common Mistakes
Watch out for these pitfalls:
The 'Random String' trap: A common brute force mistake is generating all 2²ⁿ strings of '(' and ')' and checking validity. That is O(n × 4ⁿ) work for only Catalan(n) answers. Backtracking is vastly superior.
Base Case Confusion: Remember the base case is when
open == nandclose == n(string length 2n). Don't stop just because you can't add more open brackets.Strict Order: You can NEVER add a closing parenthesis if
close >= open. This simple rule guarantees validity.
Solution
One algorithm in all six languages: the lines light up as the figure above steps.
Generate Parentheses Code
1var generateParenthesis = function(n) {2 const result = [];3 const path = [];45 const backtrack = (openCount, closeCount) => {6 if (path.length === 2 * n) {7 result.push(path.join(''));8 return;9 }10 if (openCount < n) {11 path.push('(');12 backtrack(openCount + 1, closeCount);13 path.pop();14 }15 if (closeCount < openCount) {16 path.push(')');17 backtrack(openCount, closeCount + 1);18 path.pop();19 }20 };2122 backtrack(0, 0);23 return result;24};
Generate Parentheses in Python, Java, C++, TypeScript and C
Generate Parentheses in Python
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
result = []
path = []
def backtrack(open_count: int, close_count: int) -> None:
if len(path) == 2 * n:
result.append("".join(path))
return
if open_count < n:
path.append("(")
backtrack(open_count + 1, close_count)
path.pop()
if close_count < open_count:
path.append(")")
backtrack(open_count, close_count + 1)
path.pop()
backtrack(0, 0)
return resultGenerate Parentheses in Java
class Solution {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
StringBuilder path = new StringBuilder();
backtrack(n, 0, 0, path, result);
return result;
}
private void backtrack(int n, int openCount, int closeCount, StringBuilder path, List<String> result) {
if (path.length() == 2 * n) {
result.add(path.toString());
return;
}
if (openCount < n) {
path.append('(');
backtrack(n, openCount + 1, closeCount, path, result);
path.deleteCharAt(path.length() - 1);
}
if (closeCount < openCount) {
path.append(')');
backtrack(n, openCount, closeCount + 1, path, result);
path.deleteCharAt(path.length() - 1);
}
}
}Generate Parentheses in C++
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
string path;
backtrack(n, 0, 0, path, result);
return result;
}
private:
void backtrack(int n, int openCount, int closeCount, string& path, vector<string>& result) {
if ((int)path.size() == 2 * n) {
result.push_back(path);
return;
}
if (openCount < n) {
path.push_back('(');
backtrack(n, openCount + 1, closeCount, path, result);
path.pop_back();
}
if (closeCount < openCount) {
path.push_back(')');
backtrack(n, openCount, closeCount + 1, path, result);
path.pop_back();
}
}
};Generate Parentheses in TypeScript
function generateParenthesis(n: number): string[] {
const result: string[] = [];
const path: string[] = [];
const backtrack = (openCount: number, closeCount: number): void => {
if (path.length === 2 * n) {
result.push(path.join(''));
return;
}
if (openCount < n) {
path.push('(');
backtrack(openCount + 1, closeCount);
path.pop();
}
if (closeCount < openCount) {
path.push(')');
backtrack(openCount, closeCount + 1);
path.pop();
}
};
backtrack(0, 0);
return result;
}Generate Parentheses in C
void backtrack(int n, int openCount, int closeCount, char* path, int* pathSize, char** result, int* returnSize) {
if (*pathSize == 2 * n) {
result[*returnSize] = malloc((2 * n + 1) * sizeof(char));
memcpy(result[*returnSize], path, 2 * n);
result[*returnSize][2 * n] = '\0';
(*returnSize)++;
return;
}
if (openCount < n) {
path[(*pathSize)++] = '(';
backtrack(n, openCount + 1, closeCount, path, pathSize, result, returnSize);
(*pathSize)--;
}
if (closeCount < openCount) {
path[(*pathSize)++] = ')';
backtrack(n, openCount, closeCount + 1, path, pathSize, result, returnSize);
(*pathSize)--;
}
}
char** generateParenthesis(int n, int* returnSize) {
char** result = malloc(1430 * sizeof(char*)); // Catalan(8): the most LeetCode asks for
char* path = malloc(2 * n * sizeof(char));
*returnSize = 0;
int pathSize = 0;
backtrack(n, 0, 0, path, &pathSize, result, returnSize);
free(path);
return result;
}More practice: DSA interview questions with answers · patterns cheat sheet
