Simple recursive python solution


  • 0
    N
    class Solution(object):
        def generateParenthesis(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            return self.rec(n,n,'')
                
        def rec(self, left, right, string):
            if left == 0 and right == 0:
                return [string]
            elif left == 0:
                return self.rec(left, right-1, string+')')
            elif left == right:
                return self.rec(left-1, right, string+'(')
            else:
                one = self.rec(left-1, right, string+'(')
                two = self.rec(left, right-1, string+')')
                return one + two              
    

Log in to reply
 

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