Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GenerateParentheses: add examples with explanations.
Browse files Browse the repository at this point in the history
eminencegrs committed Nov 12, 2024
1 parent efb059f commit 024d29c
Showing 2 changed files with 125 additions and 34 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
## Examples with explanations

### Example 1

Input: `n = 2`

**Explanation:**

```
GenerateParenthesis(2)
└── Handle("", 0, 0, 2)
|
├── "(" added → Handle("(", 1, 0, 2)
| |
| ├── "(" added → Handle("((", 2, 0, 2)
| | |
| | ├── ")" added → Handle("(()", 2, 1, 2)
| | | |
| | | ├── ")" added → Handle("(())", 2, 2, 2) <-- [Base case reached, add "(())" to result]
| | | |
| | | ← Backtrack to Handle("(()", 2, 1, 2)
| | |
| | ← Backtrack to Handle("((", 2, 0, 2)
| |
| ← Backtrack to Handle("(", 1, 0, 2)
|
├── ")" added → Handle("()", 1, 1, 2)
| |
| ├── "(" added → Handle("()(", 2, 1, 2)
| | |
| | ├── ")" added → Handle("()()", 2, 2, 2) <-- [Base case reached, add "()()" to result]
| | |
| | ← Backtrack to Handle("()(", 2, 1, 2)
| |
| ← Backtrack to Handle("()", 1, 1, 2)
|
← Backtrack to Handle("", 0, 0, 2)
```

### Example 2

Input: `n = 3`

**Explanation:**

```
GenerateParenthesis(3)
└── Handle("", 0, 0, 3)
|
├── "(" added → Handle("(", 1, 0, 3)
| |
| ├── "(" added → Handle("((", 2, 0, 3)
| | |
| | ├── "(" added → Handle("(((", 3, 0, 3)
| | | |
| | | ├── ")" added → Handle("((()", 3, 1, 3)
| | | | |
| | | | ├── ")" added → Handle("((())", 3, 2, 3)
| | | | | |
| | | | | ├── ")" added → Handle("((()))", 3, 3, 3) ← Base case, add "((()))" to result
| | | | | |
| | | | | ← Backtrack to Handle("((())", 3, 2, 3)
| | | | |
| | | | ← Backtrack to Handle("((()", 3, 1, 3)
| | | |
| | | ← Backtrack to Handle("(((", 3, 0, 3)
| | |
| | ├── ")" added → Handle("(()", 2, 1, 3)
| | | |
| | | ├── "(" added → Handle("(()(", 3, 1, 3)
| | | | |
| | | | ├── ")" added → Handle("(()()", 3, 2, 3)
| | | | | |
| | | | | ├── ")" added → Handle("(()())", 3, 3, 3) ← Base case, add "(()())" to result
| | | | | |
| | | | | ← Backtrack to Handle("(()()", 3, 2, 3)
| | | | |
| | | | ← Backtrack to Handle("(()(", 3, 1, 3)
| | | |
| | | ← Backtrack to Handle("(()", 2, 1, 3)
| | |
| | ├── ")" added → Handle("(())", 2, 2, 3)
| | | |
| | | ├── "(" added → Handle("(())(", 3, 2, 3)
| | | | |
| | | | ├── ")" added → Handle("(())()", 3, 3, 3) ← Base case, add "(())()" to result
| | | | |
| | | | ← Backtrack to Handle("(())(", 3, 2, 3)
| | | |
| | | ← Backtrack to Handle("(())", 2, 2, 3)
| | |
| | ← Backtrack to Handle("((", 2, 0, 3)
| |
| ← Backtrack to Handle("(", 1, 0, 3)
|
├── ")" added → Handle("()", 1, 1, 3)
| |
| ├── "(" added → Handle("()(", 2, 1, 3)
| | |
| | ├── "(" added → Handle("()((", 3, 1, 3)
| | | |
| | | ├── ")" added → Handle("()(()", 3, 2, 3)
| | | | |
| | | | ├── ")" added → Handle("()(())", 3, 3, 3) ← Base case, add "()(())" to result
| | | | |
| | | | ← Backtrack to Handle("()(()", 3, 2, 3)
| | | |
| | | ← Backtrack to Handle("()((", 3, 1, 3)
| | |
| | ├── ")" added → Handle("()()", 2, 2, 3)
| | | |
| | | ├── "(" added → Handle("()()(", 3, 2, 3)
| | | | |
| | | | ├── ")" added → Handle("()()()", 3, 3, 3) ← Base case, add "()()()" to result
| | | | |
| | | | ← Backtrack to Handle("()()(", 3, 2, 3)
| | | |
| | | ← Backtrack to Handle("()()", 2, 2, 3)
| | |
| | ← Backtrack to Handle("()(", 2, 1, 3)
| |
| ← Backtrack to Handle("()", 1, 1, 3)
|
← Backtrack to Handle("", 0, 0, 3)
```

0 comments on commit 024d29c

Please sign in to comment.