Generate Parentheses
To solve this coding challenge, we need to generate all combinations of well-formed parentheses given
pairs of parentheses. A combination of well-formed parentheses means that each opening parenthesis '(' has a corresponding closing parenthesis ')' and they are arranged correctly. This is a typical backtracking problem where we will build the combinations incrementally and backtrack when the current combination is invalid.
n
Explanation
The primary constraint is we need balanced parentheses, meaning each opening parenthesis must have a closing one and the count of closing parentheses should never exceed the count of opening ones at any point in the string. In this solution, we employ a recursive backtracking method:-
We use a recursive helper function
that takes current string
backtrack(), the number of open parenthesescurr, and the number of close parenthesesopen_countas parameters.close_count -
The base case of this recursion is when the length of the current string `curr` is `2
n
n
(since we have2 n`).pairs of parentheses, so the total length should be -
If the number of open parentheses is less than
, we add an open parenthesis and recursively call
n.backtrack() -
If the number of close parentheses is less than the number of open parentheses, we add a close parenthesis and recursively call
.
backtrack()
Detailed Steps in Pseudocode:
-
Initialize an empty list
to store the final combinations.
result -
Define a recursive function
:
backtrack() -
Start the recursive process by calling
.
backtrack(result, "", 0, 0) - Initialization :
-
Create the function
that accepts an integer
generateParenthesis.n -
Inside this function, initialize an empty list
to store valid combinations of parentheses.
result - Defining Backtracking Function :
-
Define an inner function
that takes four parameters: the result list
backtrack, the current string being builtresult, the number of open parentheses added so farcurr, and the number of close parentheses added so faropen_count.close_count - Base Case in Recursion :
-
Within
, check if the length of the current string
backtrackiscurr. If true, it means the current string is a complete valid combination. Append this string to2 * nand return.result - Recursive Calls for Opening Parentheses :
-
Check if the
is less than
open_count. If true, it means we can add more opening parentheses. Callnrecursively withbacktrackconcatenated with '(',curr, andopen_count + 1.close_count - Recursive Calls for Closing Parentheses :
-
Check if the
is less than
close_count. If true, it means we can add a closing parenthesis while still maintaining the validity of the combination. Callopen_countrecursively withbacktrackconcatenated with ')',curr, andopen_count.close_count + 1 - Starting the Recursion :
-
Start the backtracking process by calling
from within the
backtrack(result, "", 0, 0)function. This initializes the recursion with an empty string and zero counts for both open and close parentheses.generateParenthesis - Returning the Result :
-
Finally, return the
list which contains all the valid combinations of well-formed parentheses.
result
-
If the length of `curr` is `2
n`, append `curr` to `result` and return.
* If
open_count
n
backtrack()
open_count
close_count
open_count
backtrack()
close_count
Pseudocode
# Initialize the primary function to generate parentheses
function generateParenthesis(n):
# Initialize a list to hold the combinations
result = []
# Define the backtracking function to build valid combinations
function backtrack(result, curr, open_count, close_count):
# Base case: if the current combination length is 2 * n, it's complete
if length(curr) == 2 * n:
# Append the current combination to the result list
append result with curr
return
# If more open parentheses can be added
if open_count < n:
# Add an open parenthesis and recursively call backtrack
backtrack(result, concatenate(curr, '('), open_count + 1, close_count)
# If adding a close parenthesis still maintains a valid combination
if close_count < open_count:
# Add a close parenthesis and recursively call backtrack
backtrack(result, concatenate(curr, ')'), open_count, close_count + 1)
# Start the backtracking with an empty string and counts set to zero
backtrack(result, "", 0, 0)
# Return the list of valid combinations
return result
# Example usage
output1 = generateParenthesis(3)
# Expected: ["((()))","(()())","(())()","()(())","()()()"]
output2 = generateParenthesis(1)
# Expected: ["()"]