-
Notifications
You must be signed in to change notification settings - Fork 0
/
021 Generate Parentheses.py
executable file
·41 lines (35 loc) · 1.23 KB
/
021 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
"""
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
"""
__author__ = 'Danyang'
class Solution:
def generateParenthesis(self, n):
"""
:param n: integer
:return: list of string
"""
result = []
self.generateParenthesisDfs(result, "", n, n)
return result
def generateParenthesisDfs(self, result, cur, left, right):
"""
DFS
Catalan Number
:param result: result list
:param cur: currently processing string
:param left: number of left parenthesis remaining
:param right: number of right parenthesis remaining
"""
if left == 0 and right == 0:
result.append(cur)
return
# add left parenthesis
if left > 0:
self.generateParenthesisDfs(result, cur + "(", left - 1, right)
# add right parenthesis
if right > left:
self.generateParenthesisDfs(result, cur + ")", left, right - 1)
if __name__=="__main__":
assert Solution().generateParenthesis(3)==['((()))', '(()())', '(())()', '()(())', '()()()']