You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0022_generate_parentheses/readme.md
+58-1Lines changed: 58 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,4 +18,61 @@ Given `n` pairs of parentheses, write a function to _generate all combinations o
18
18
19
19
**Constraints:**
20
20
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.
0 commit comments