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,
- Leetcode Generate Parentheses problem solution in C++
- Leetcode Generate Parentheses problem solution in Java
- Leetcode Generate Parentheses problem solution in C
- Leetcode Generate Parentheses problem solution in C#