forked from Arnon120/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22. Generate Parentheses.py
59 lines (47 loc) · 1.43 KB
/
22. 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
54
55
56
57
58
59
class Solution:
def generate_Parenthesis_substring(self, pairs: int, opened: int):
if pairs == 0 and opened == 0:
return [""]
if opened > 0:
l_1 = [")" + val for val in self.generate_Parenthesis_substring(pairs , opened - 1)]
else:
l_1 = []
if pairs > 0:
l_2 = ["(" + val for val in self.generate_Parenthesis_substring(pairs - 1 , opened + 1)]
else:
l_2 = []
return l_2 + l_1
def generateParenthesis(self, n: int):
if n == 0:
return []
else:
return self.generate_Parenthesis_substring(n, 0)
"""
sol = Solution()
n = 3
l = sol.generateParenthesis(n)
print(l)
"""
"""
Also uses recursion but!
the append is alot simpler: pre + ')'
recusion is left to right - not right to left...
uses the "pre" in a smart way.
less cases?
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
def dfs(left, right, pre):
if left == right == 0:
res.append(pre)
# the situation left larger than right is impossible to construct
if left > right:
return
# only able to add ")"
if left < right:
dfs(left, right - 1, pre + ')')
if left != 0:
dfs(left - 1, right, pre + '(')
dfs(n, n, '')
return res
"""