-
Notifications
You must be signed in to change notification settings - Fork 0
/
22_GenerateParenthese.py
33 lines (29 loc) · 977 Bytes
/
22_GenerateParenthese.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
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
# Backtracking
# Only add when we know the string will be remain valid
# Keep tracking the number of openning and closing brackets
# Check the condition and pass with appended (, ) to next backtracking
# Pruning is with two conditions to validate
result = []
def backtrack(s, left, right):
if len(s) == 2 * n:
result.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([], 0, 0)
return result
if __name__ == '__main__':
n = 3
print(Solution().generateParenthesis(n))