-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_parentheses.py
45 lines (39 loc) · 1.57 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
'''
Question: https://leetcode.com/problems/generate-parentheses/
'''
from typing import List
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
'''
RULE 1. Only add open parenthese if opened < n
RULE 2. Only add close parenthese if closed < opened
RULE 3. Valid ONLY IF opened == closed == n
'''
# define lists for storing single valid paranthese in `stack` and the result in `res`
stack, res = [], []
def validParanthese(opened: int, closed: int) -> None:
'''
Follow the above rules to generate a valid paranthese
'''
# RULE 3: base case
if opened == closed == n:
# append the valid paranthese as a string to `res`
res.append("".join(stack))
return
# RULE 1
if opened < n:
stack.append("(")
# since we added an opening bracket, increment opened count by 1
validParanthese(opened + 1, closed)
# remove the added valid paranthese from the stack after appending to res,
# since we are using `stack` as a global variable
stack.pop()
# RULE 2
if closed < opened:
stack.append(")")
# since we added an closing bracket, increment closed count by 1
validParanthese(opened, closed + 1)
stack.pop()
# initial call with 0 opened and 0 closed
validParanthese(0, 0)
return res