-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0022. Generate Parentheses.py
53 lines (46 loc) · 1.43 KB
/
0022. Generate Parentheses.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
#
# Example 1:
#
# Input: n = 3
# Output: ["((()))","(()())","(())()","()(())","()()()"]
#
# Example 2:
#
# Input: n = 1
# Output: ["()"]
#
# Constraints:
# 1 <= n <= 8
# 1) Backtracking
# The complexity analysis rests on understanding how many elements there are in generateParenthesis(n). It turns out
# this is the n-th Catalan number 1 / n+1 (2n n), which is bounded asymptotically by 4^n / (n * sqrt(n))
# Time O(4^n / sqrt(n)). Each valid sequence has at most n steps during the backtracking procedure.
# Space O(4^n / sqrt(n)). As described above, and using O(n) space to store the sequence.
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
def go(l, r, s):
if len(s) == 2 * n:
res.append(s)
return
if l < n:
go(l + 1, r, s + "(")
if r < l:
go(l, r + 1, s + ")")
go(0, 0, "")
return res
# 2) Backtracking, similar to 1)
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
def go(l, r, s):
if l == 0 and r == 0:
res.append(s)
return
if l > 0:
go(l - 1, r, s + "(")
if r > l:
go(l, r - 1, s + ")")
go(n, n, "")
return res