[nb] Commit

This commit is contained in:
2021-08-08 10:53:32 -05:00
parent a8716f9734
commit f9c2c88398
3 changed files with 78 additions and 43 deletions

View File

@@ -0,0 +1,25 @@
from typing import List
# ()()()
# (())()
# ((()))
# ()(())
# (()())
def generateParenthesis(n: int) -> List[str]:
ans = []
def backtrack(S = [], left = 0, right = 0):
if len(S) == 2 * n:
ans.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()
return ans
generateParenthesis(3)