Another python solution


  • 0
    M
    class Solution:
        # @param an integer
        # @return a list of string
        def helper(self, current, opened, closed, n):
            if n == 0:
                return []
            if opened == closed == n:
                return [current]
            o, c = [],[]
            out = []
            if opened < n:
                o = self.helper(current + '(', opened + 1, closed, n)
            if opened > closed:
                c = self.helper(current + ')', opened, closed + 1, n)
            return o + c
            
        def generateParenthesis(self, n):
            return self.helper('', 0, 0, n)

Log in to reply
 

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