LeetCode 22 - Generate Parentheses Problem

Problem Statement

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

Solution:

class Solution: def generateParenthesis(self, n: int) -> List[str]: output = [] self.backtrack(output, '' ,0, 0, n) return output def backtrack(self, output, current_string ,opening, closing, maximum): if len(current_string) == maximum*2: output.append(current_string) return if opening<maximum: self.backtrack(output, current_string+'(', opening+1, closing, maximum) if closing<opening: self.backtrack(output, current_string+')', opening ,closing+1, maximum)