Medium
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example 1:
Input: n = 3
Output: [”((()))”,”(()())”,”(())()”,”()(())”,”()()()”]
Example 2:
Input: n = 1
Output: [”()”]
Constraints:
1 <= n <= 8Here are the steps to solve the “Generate Parentheses” problem:
result to store the generated combinations.generateParenthesisHelper that takes parameters:
current: the current combination being built.left: the remaining count of open parentheses.right: the remaining count of close parentheses.left and right become zero, add the current combination to the result list.left is greater than zero, make a recursive call with one less open parenthesis and the current combination appended with an open parenthesis.right is greater than left, make a recursive call with one less close parenthesis and the current combination appended with a close parenthesis.generateParenthesisHelper function with an empty string (current), the total number of open parentheses (n), and the total number of close parentheses (n).result list containing all the valid combinations.class Solution:
def generateParenthesis(self, n: int):
def generateParenthesisHelper(current, left, right):
# Base case: If both left and right are zero, add the current combination to result
if left == 0 and right == 0:
result.append(current)
return
# Recursive calls
if left > 0:
generateParenthesisHelper(current + '(', left - 1, right)
if right > left:
generateParenthesisHelper(current + ')', left, right - 1)
# Initialize result list
result = []
# Start recursion
generateParenthesisHelper('', n, n)
# Return result list
return result
# Example Usage:
solution = Solution()
# Example 1:
n1 = 3
result1 = solution.generateParenthesis(n1) # Output: ["((()))","(()())","(())()","()(())","()()()"]
# Example 2:
n2 = 1
result2 = solution.generateParenthesis(n2) # Output: ["()"]
This code defines a Solution class with a method generateParenthesis that takes an integer n as input and returns a list containing all combinations of well-formed parentheses for n pairs. The example usage demonstrates how to create an instance of the Solution class and call the generateParenthesis method with different inputs.