-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_parentheses_0022.py
More file actions
33 lines (28 loc) · 1.17 KB
/
generate_parentheses_0022.py
File metadata and controls
33 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def generateParenthesis(self, n: int) -> list[str]:
"""
Generate all valid combinations of n pairs of parentheses.
Uses backtracking: build combinations by adding '(' or ')'
until the length reaches 2*n.
Validity rules:
- '(' can be added if open_count < n
- ')' can be added if close_count < open_count
Args:
n: Number of pairs of parentheses (1 <= n <= 8)
Returns:
List of all valid combinations of parentheses
Time Complexity: O(4^n / sqrt(n)) - Catalan number
Space Complexity: O(n) - recursion depth
"""
open_bracket, close_bracket = '(', ')'
result: list[str] = []
def backtrack(current: str, open_count: int, close_count: int) -> None:
if len(current) == n * 2:
result.append(current)
return
if open_count < n:
backtrack(current + open_bracket, open_count + 1, close_count)
if close_count < open_count:
backtrack(current + close_bracket, open_count, close_count + 1)
backtrack('', 0, 0)
return result