Skip to content

Latest commit

 

History

History
19 lines (18 loc) · 538 Bytes

22.md

File metadata and controls

19 lines (18 loc) · 538 Bytes

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 + ")")