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 for a given number of pairs. This problem is significant in various applications such as compiler design, expression parsing, and validating mathematical expressions. 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 avoids generating invalid strings in the first place.
Here is a step-by-step breakdown of the backtracking algorithm:
import java.util.ArrayList;
import java.util.List;
public class GenerateParentheses {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}
private void backtrack(List<String> result, String current, int open, int close, int max) {
// If the current string is of the maximum length, add it to the result
if (current.length() == max * 2) {
result.add(current);
return;
}
// If we can add an opening parenthesis, do so
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
// If we can add a closing parenthesis, do so
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
GenerateParentheses gp = new GenerateParentheses();
System.out.println(gp.generateParenthesis(3));
}
}
The time complexity of this approach is O(4^n / √n), which is derived from the Catalan number. The space complexity is O(n) due to the recursion stack.
Some potential edge cases include:
These edge cases are handled by the algorithm as it ensures that the number of opening and closing parentheses are balanced.
To test the solution comprehensively, you can use a variety of test cases:
JUnit or any other testing framework can be used to automate these tests.
When approaching such problems, it is essential to:
Generating well-formed parentheses is a classic problem that helps in understanding backtracking and recursion. By practicing such problems, you can improve your problem-solving skills and gain a deeper understanding of algorithm design.
For further reading and practice, consider the following resources: