A recursive Python solution - 66ms


  • 0
    G
    class Solution:
        # @param n, an integer
        # @return a string[]
        def generateParenthesis(self, n, openCount = 0, closeCount = 0, output = None, temp = None):
            if output is None or temp is None:
                output, temp = [], []
    
            if len(temp) == 2 * n:
                output.append(''.join(temp))
                return
    
            if openCount < n:
                temp.append('(')
                openCount += 1
                self.generateParenthesis(n, openCount, closeCount, output, temp)
                temp.pop()
                openCount -= 1
    
            if closeCount < n and closeCount < openCount:
                temp.append(')')
                closeCount += 1
                self.generateParenthesis(n, openCount, closeCount, output, temp)
                temp.pop()
                closeCount -= 1
    
            return output

Log in to reply
 

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