-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path22.py
37 lines (29 loc) · 830 Bytes
/
22.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
class Solution:
def __init__(self):
self.ans = []
def bt(self, arr, k, lc, rc):
if k==len(arr):
if lc == rc:
p = arr[:]
p1 = "".join(arr)
self.ans.append(p1)
else:
c = ["(", ")"]
for i in range(len(c)):
if c[i]=="(":
arr[k]=c[i]
self.bt(arr, k+1, lc+1, rc)
elif c[i]==")" and rc < lc:
arr[k]=c[i]
self.bt(arr, k+1, lc, rc+1)
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
arr = [""]*(2*n)
self.bt(arr, 0, 0, 0)
return self.ans
if __name__ == "__main__":
s = Solution()
print(s.generateParenthesis(3))