-
Notifications
You must be signed in to change notification settings - Fork 43
/
generate-parentheses.py
250 lines (221 loc) · 6.75 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
22. Generate Parentheses
Medium
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
"""
# V0
# IDEA : bracktrack + Valid Parentheses (LC 020)
class Solution(object):
def generateParenthesis(self, n):
# help func for backtracking
def help(tmp):
if len(tmp) == n * 2 and check(tmp):
res.append(tmp)
return
if len(tmp) == n * 2:
return
"""
NOTE !!! via "for l in _list" + recursion, we can for "endless" for loop over element in _list till terminate confition is met
"""
for l in _list:
print ("l = " + str(l))
help(tmp + l) ### NOTE !!! we HAVE TO use "tmp + l" instead of "tmp += l"
# below is OK as well
#_tmp = tmp + l
#help(_tmp)
"""
LC 020 Valid Parentheses
"""
def check(s):
lookup = {"(":")", "[":"]", "{":"}"}
q = []
for i in s:
if i not in lookup and len(q) == 0:
return False
elif i in lookup:
q.append(i)
else:
tmp = q.pop()
if lookup[tmp] != i:
return False
return True if len(q) == 0 else False
_list = ['(', ')']
if n == 1:
return ["()"]
res = []
tmp = ""
help(tmp)
return res
# V0'
# https://blog.csdn.net/fuxuemingzhu/article/details/79362373
# IDEA: BACKTRACKING + DFS
# NOTE : KEEP DFS WHEN MEAT 2 CONDTIONS:
# 1) len(path) < n
# 2) # of "(" > # of ")" (means it's still possible to form a "paratheses" as expected)
class Solution(object):
def generateParenthesis(self, n):
res = []
self.dfs(res, n, n, '')
return res
def dfs(self, res, left, right, path):
if left == 0 and right == 0:
res.append(path)
return
if left > 0:
self.dfs(res, left - 1, right, path + '(')
if left < right:
self.dfs(res, left, right - 1, path + ')')
# V1
# IDEA : DFS
# https://blog.csdn.net/nxhyd/article/details/72514987
# https://blog.csdn.net/fuxuemingzhu/article/details/79362373
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
self.dfs(res, n, n, '')
return res
def dfs(self, res, left, right, path):
if left == 0 and right == 0:
res.append(path)
return
if left > 0:
self.dfs(res, left - 1, right, path + '(')
if left < right:
self.dfs(res, left, right - 1, path + ')')
# V1'
# BRUTE FORCE
# https://leetcode.com/problems/generate-parentheses/solution/
class Solution(object):
def generateParenthesis(self, n):
def generate(A = []):
if len(A) == 2*n:
if valid(A):
ans.append("".join(A))
else:
A.append('(')
generate(A)
A.pop()
A.append(')')
generate(A)
A.pop()
def valid(A):
bal = 0
for c in A:
if c == '(': bal += 1
else: bal -= 1
if bal < 0: return False
return bal == 0
ans = []
generate()
return ans
# V1''
# https://leetcode.com/problems/generate-parentheses/solution/
# BRACKTRACKING
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
ans = []
def backtrack(S = [], left = 0, right = 0):
if len(S) == 2 * n:
ans.append("".join(S))
return
if left < n:
S.append("(")
backtrack(S, left+1, right)
S.pop()
if right < left:
S.append(")")
backtrack(S, left, right+1)
S.pop()
backtrack()
return ans
# V1'''
# https://leetcode.com/problems/generate-parentheses/solution/
# Closure Number
class Solution(object):
def generateParenthesis(self, N):
if N == 0: return ['']
ans = []
for c in xrange(N):
for left in self.generateParenthesis(c):
for right in self.generateParenthesis(N-1-c):
ans.append('({}){}'.format(left, right))
return ans
# V1'''''
# https://blog.csdn.net/nxhyd/article/details/72514987
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
def generate(p, left, right):
if right >= left >= 0:
if not right:
yield p
for q in generate(p + '(', left - 1, right) : yield q
for q in generate(p + ')', left, right - 1) : yield q
return list(generate('', n, n))
# V1''''''
# https://blog.csdn.net/nxhyd/article/details/72514987
class Solution(object):
def deep(self,tot,ln,rn,strs,lis):
if ln<tot:
self.deep(tot,ln+1,rn,strs+'(',lis)
if rn<tot and rn<ln:
self.deep(tot,ln,rn+1,strs+')',lis)
if ln==tot and rn==tot:
lis.append(strs)
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
lis=[]
self.deep(n,0,0,'',lis)
return lis
# V1''''''
# https://blog.csdn.net/nxhyd/article/details/72514987
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
def genrate(p,left,right,parens=[]):
if left:
genrate(p+ '(',left-1,right)
if right>left:
genrate(p+')',left,right-1)
if not right:
parens+=p,
return parens
return genrate('',n, n)
# V2
# Time: O(4^n / n^(3/2)) ~= Catalan numbers
# Space: O(n)
class Solution(object):
# @param an integer
# @return a list of string
def generateParenthesis(self, n):
result = []
self.generateParenthesisRecu(result, "", n, n)
return result
def generateParenthesisRecu(self, result, current, left, right):
if left == 0 and right == 0:
result.append(current)
if left > 0:
self.generateParenthesisRecu(result, current + "(", left - 1, right)
if left < right:
self.generateParenthesisRecu(result, current + ")", left, right - 1)