forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.py
36 lines (28 loc) · 695 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
'''
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
'''
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
result = []
def backtracking(S, left, right):
if len(S) == 2*n:
result.append(S)
return
if left < n:
backtracking(S+'(', left+1, right)
if right < left:
backtracking(S+')', left, right+1)
backtracking('', 0, 0)
return result