24ms beat:99%
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
if n==0: return []
left, right, ans = n, n, []
self.dfs(left, right, ans, "")
return ans
def dfs(self, left, right, ans, string):
if right < left: return
if left == 0 and right == 0:
ans.append(string)
return
if left:
self.dfs(left-1, right, ans, string + "(")
if right:
self.dfs(left, right-1, ans, string + ")")