Leetcode Generate Parentheses problem solution in Python programming

In the Leetcode Generate Parentheses problem solution in Python programming Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Leetcode Generate Parentheses problem solution in Python programming

class Solution(object):
    def generateParenthesis(self, n):
        answerArray = []
        
        def genParen(needOpen, needClose, accum):
            
            if needOpen == 0 and needClose == 0:
                answerArray.append(accum)
                return
            
            if needOpen >= 1:
                genParen(needOpen - 1, needClose + 1, accum + "(")
                
            if needClose >= 1:
                genParen(needOpen, needClose - 1, accum + ")")
                
        genParen(n, 0, "")
        return answerArray

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *