-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path22-generate-parentheses.java
More file actions
59 lines (50 loc) · 2.05 KB
/
22-generate-parentheses.java
File metadata and controls
59 lines (50 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.List;
class Solution {
// Approach: Backtracking
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 currentString,
int openCount,
int closeCount,
int maxPairs
) {
// Base case: If the current string length is equal to 2 * maxPairs,
// it means we have a complete combination.
if (currentString.length() == maxPairs * 2) {
result.add(currentString);
return;
}
// Recursive case 1: Add an opening parenthesis if we still have open parentheses to add.
// The number of open parentheses must not exceed maxPairs.
if (openCount < maxPairs) {
backtrack(result, currentString + '(', openCount + 1, closeCount, maxPairs);
}
// Recursive case 2: Add a closing parenthesis if the number of closing parentheses
// is less than the number of open parentheses. This ensures well-formedness.
if (closeCount < openCount) {
backtrack(result, currentString + ')', openCount, closeCount + 1, maxPairs);
}
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println("Testing Generate Parentheses:");
// Test 1
int n1 = 3;
List<String> result1 = sol.generateParenthesis(n1);
System.out.println("Input: n = " + n1 + " -> Output: " + result1 + " (Expected: [((())), (()()), ()(()), (())(), ()()()])");
// Test 2
int n2 = 1;
List<String> result2 = sol.generateParenthesis(n2);
System.out.println("Input: n = " + n2 + " -> Output: " + result2 + " (Expected: [()])");
// Test 3
int n3 = 2;
List<String> result3 = sol.generateParenthesis(n3);
System.out.println("Input: n = " + n3 + " -> Output: " + result3 + " (Expected: [(()), ()()])");
}
}