Recursive Python solution


  • 0
    C
    class Solution(object):
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        res = []
        
        
        def dfs(open_count, remaining, currString):
            
            if not remaining > 0 and not open_count > 0:
                 res.append(currString)
    
            elif open_count > 0:
                dfs(open_count - 1, remaining, currString + ")")
            if remaining > 0:
                dfs(open_count + 1, remaining - 1, currString + "(")
        
        dfs(0, n, "")
       
        return res

Log in to reply
 

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