-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgenerate-parentheses.py
More file actions
30 lines (24 loc) · 890 Bytes
/
generate-parentheses.py
File metadata and controls
30 lines (24 loc) · 890 Bytes
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
from functools import lru_cache
class Solution:
def generateParenthesis(self, n):
result = []
check = []
open_left = close_left = n
@lru_cache(None)
def find_valid(res_str, open_left, close_left):
if open_left == 0 and close_left == 0 and len(check) == 0:
result.append(res_str)
if open_left > 0:
res_str += "("
check.append("(")
find_valid(res_str, open_left - 1, close_left)
res_str = res_str[:-1]
check.pop()
if check and check[-1] == "(" and close_left > 0:
res_str += ")"
check.pop()
find_valid(res_str, open_left, close_left - 1)
res_str = res_str[:-1]
check.append("(")
find_valid("", n, n)
return result