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
backtrack()
curr
open_count
close_count
-
The base case of this recursion is when the length of the current string `curr` is `2
n
(since we have
pairs of parentheses, so the total length should be
-
If the number of open parentheses is less than
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
result
-
Define a recursive function
backtrack()
-
Start the recursive process by calling
backtrack(result, "", 0, 0)
- Initialization :
-
Create the function
generateParenthesis
n
-
Inside this function, initialize an empty list
result
- Defining Backtracking Function :
-
Define an inner function
backtrack
result
curr
open_count
close_count
- Base Case in Recursion :
-
Within
backtrack
curr
2 * n
result
- Recursive Calls for Opening Parentheses :
-
Check if the
open_count
n
backtrack
curr
open_count + 1
close_count
- Recursive Calls for Closing Parentheses :
-
Check if the
close_count
open_count
backtrack
curr
open_count
close_count + 1
- Starting the Recursion :
-
Start the backtracking process by calling
backtrack(result, "", 0, 0)
generateParenthesis
- Returning the Result :
-
Finally, return the
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: ["()"]