Skip to content

Commit 448f37b

Browse files
authored
Enhance readme with generate parentheses solution
Added detailed explanation and implementation for generating parentheses using backtracking in Java.
1 parent 1564190 commit 448f37b

File tree

1 file changed

+58
-1
lines changed
  • src/main/java/g0001_0100/s0022_generate_parentheses

1 file changed

+58
-1
lines changed

src/main/java/g0001_0100/s0022_generate_parentheses/readme.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,61 @@ Given `n` pairs of parentheses, write a function to _generate all combinations o
1818

1919
**Constraints:**
2020

21-
* `1 <= n <= 8`
21+
* `1 <= n <= 8`
22+
23+
To solve the "Generate Parentheses" problem in Java with a `Solution` class, we can use a backtracking approach. Here are the steps:
24+
25+
1. Define a `Solution` class.
26+
2. Define a method named `generateParenthesis` that takes an integer `n` as input and returns a list of strings representing all combinations of well-formed parentheses.
27+
3. Create an empty list to store the result.
28+
4. Call the recursive helper function `generateParenthesisHelper` with the empty string `""`, counts of open and close parentheses set to `0`, the value of `n`, and the result list.
29+
5. In the `generateParenthesisHelper` function:
30+
- If the length of the current string is equal to `2 * n`, add it to the result list.
31+
- If the count of open parentheses is less than `n`, append an open parenthesis to the current string and call the function recursively with increased open count.
32+
- If the count of close parentheses is less than the count of open parentheses, append a close parenthesis to the current string and call the function recursively with increased close count.
33+
6. Return the result list.
34+
35+
Here's the implementation:
36+
37+
```java
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
41+
public class Solution {
42+
public List<String> generateParenthesis(int n) {
43+
List<String> result = new ArrayList<>();
44+
generateParenthesisHelper("", 0, 0, n, result);
45+
return result;
46+
}
47+
48+
private void generateParenthesisHelper(String current, int open, int close, int n, List<String> result) {
49+
if (current.length() == 2 * n) {
50+
result.add(current);
51+
return;
52+
}
53+
54+
if (open < n) {
55+
generateParenthesisHelper(current + "(", open + 1, close, n, result);
56+
}
57+
58+
if (close < open) {
59+
generateParenthesisHelper(current + ")", open, close + 1, n, result);
60+
}
61+
}
62+
63+
public static void main(String[] args) {
64+
Solution solution = new Solution();
65+
66+
// Test cases
67+
int n1 = 3;
68+
System.out.println("Parentheses combinations for n = " + n1 + ":");
69+
System.out.println(solution.generateParenthesis(n1));
70+
71+
int n2 = 1;
72+
System.out.println("\nParentheses combinations for n = " + n2 + ":");
73+
System.out.println(solution.generateParenthesis(n2));
74+
}
75+
}
76+
```
77+
78+
This implementation provides a solution to the "Generate Parentheses" problem in Java using a backtracking approach.

0 commit comments

Comments
 (0)