Given n
pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
A parentheses string is well-formed if and only if:
It is the empty string, or
It can be written as AB (A concatenated with B), where A and B are well-formed, or
It can be written as (A), where A is well-formed.
Example 1:
Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1 Output: ["()"]
Note: 1 <= n <= 8
The core challenge of this problem is to generate all possible combinations of well-formed parentheses given n
pairs. This problem is significant in various applications such as validating expressions, parsing, and compiler design. A common pitfall is to generate invalid combinations or miss some valid ones.
To solve this problem, we can use a backtracking approach. The idea is to build the string step by step, ensuring at each step that the string remains valid. We can start with an empty string and add either an opening or closing parenthesis, ensuring that the number of closing parentheses never exceeds the number of opening ones.
A naive solution would be to generate all possible strings of length 2n
and then filter out the valid ones. However, this approach is not optimal as it generates many invalid strings and has a high time complexity.
The optimized solution uses backtracking to generate only valid strings. This approach is more efficient as it prunes invalid paths early.
Here is a step-by-step breakdown of the backtracking algorithm:
open_count
and close_count
.open_count
is less than n
.close_count
is less than open_count
.2n
.2n
, add it to the result list.def generate_parentheses(n):
def backtrack(s, open_count, close_count):
# If the current string s has reached the maximum length, add it to the result
if len(s) == 2 * n:
result.append(s)
return
# If we can add an opening parenthesis, do so
if open_count < n:
backtrack(s + '(', open_count + 1, close_count)
# If we can add a closing parenthesis, do so
if close_count < open_count:
backtrack(s + ')', open_count, close_count + 1)
result = []
backtrack('', 0, 0)
return result
# Example usage
print(generate_parentheses(3)) # Output: ["((()))","(()())","(())()","()(())","()()()"]
The time complexity of this approach is O(4^n / sqrt(n)), which is derived from the Catalan number. The space complexity is O(n) due to the recursion stack.
Some potential edge cases include:
n = 0
: The output should be an empty list.n = 1
: The output should be ["()"].These cases are handled naturally by the algorithm.
To test the solution comprehensively, consider the following test cases:
n = 1
and n = 2
.n = 0
.n
to ensure performance.Using a testing framework like unittest
in Python can help automate these tests.
When approaching such problems, consider the following tips:
Practice solving similar problems to improve your problem-solving skills.
Generating well-formed parentheses is a classic problem that helps in understanding recursion and backtracking. By practicing such problems, you can improve your algorithmic thinking and problem-solving skills.
For further reading and practice, consider the following resources: