22. Generate Parentheses
1. ((()))
2. (()())
3. (())()
4. ()(())
5. ()()()class Solution:
def generateParenthesis(self, n: int) -> List[str]:
ans = []
def backtrack(curr = [], left = 0, right = 0):
if left == n and right == n:
ans.append(''.join(curr))
return
if left < n:
curr.append('(')
backtrack(curr, left + 1, right)
curr.pop()
if right < left:
curr.append(')')
backtrack(curr, left, right + 1)
curr.pop()
backtrack()
return ansLast updated