My python solution, idea is clear, codes are concise


  • 2
    G
    enter code hereclass Solution(object):
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        res=[]
        self.helper('',res,n,0,0)
        return res  
    
    def helper(self,st,res,max,left,right):
        if right==max:
            res.append(st)
        if left<max:
            self.helper(st+'(',res,max,left+1,right)
        if right<left:
            self.helper(st+')',res,max,left,right+1)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.