Skip to content

Commit ae4e1b9

Browse files
Time: 4 ms (13.52%) | Memory: 19.8 MB (5.72%) - LeetSync
1 parent c2edd5f commit ae4e1b9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def prettyPrint(l):
2+
return ["".join(e) for e in l]
3+
4+
def recursive_generation(c, start, end):
5+
c = c.copy()
6+
7+
if start >= end:
8+
return [c]
9+
10+
c[start] = "("
11+
12+
filled_lists = []
13+
14+
for i in range(int((end - start) / 2)):
15+
c[start + 1 + i * 2] = ")"
16+
17+
bracket_lists = recursive_generation(c, start + 1, start + 1 + i * 2)
18+
19+
for bracket_list in bracket_lists:
20+
filled_lists += recursive_generation(bracket_list, start + 2 + i * 2, end)
21+
22+
c[start + 1 + i * 2] = "."
23+
24+
return filled_lists
25+
26+
27+
28+
class Solution:
29+
def generateParenthesis(self, n: int) -> List[str]:
30+
# [(, 0, 0, 0, 0, 0]
31+
32+
# [(, ), 0, 0, 0, 0]
33+
# [(, 0, 0, ), 0, 0]
34+
# [(, 0, 0, 0, 0, )]
35+
36+
37+
return prettyPrint(recursive_generation(["."] * n * 2, 0, n * 2))

0 commit comments

Comments
 (0)