-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parentheses.py
25 lines (25 loc) · 875 Bytes
/
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
# Problem: https://leetcode.com/problems/generate-parentheses/description/
class Solution(object):
s = ''
def generateParenthesisUtil(self, n, openParen, closeParen, res):
if openParen == 0:
self.s += ')'*closeParen
res.append(self.s)
self.s = self.s[:-closeParen]
return
if openParen > 0:
self.s += '('
self.generateParenthesisUtil(n, openParen-1, closeParen, res)
self.s = self.s[:-1]
if openParen < closeParen:
self.s += ')'
self.generateParenthesisUtil(n, openParen, closeParen-1, res)
self.s = self.s[:-1]
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
self.generateParenthesisUtil(n, n, n, res)
return res