-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-parentheses.java
More file actions
31 lines (27 loc) · 986 Bytes
/
Copy pathgenerate-parentheses.java
File metadata and controls
31 lines (27 loc) · 986 Bytes
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
import java.util.*;
class Solution {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, new StringBuilder(), n, 0, 0);
return result;
}
private void backtrack(List<String> result, StringBuilder cur, int n, int open, int close) {
// Base case: if the current string has used all parentheses
if (cur.length() == 2 * n) {
result.add(cur.toString());
return;
}
// Add '(' if we still have open parentheses available
if (open < n) {
cur.append('(');
backtrack(result, cur, n, open + 1, close);
cur.setLength(cur.length() - 1); // backtrack
}
// Add ')' if there are unmatched '(' to close
if (close < open) {
cur.append(')');
backtrack(result, cur, n, open, close + 1);
cur.setLength(cur.length() - 1); // backtrack
}
}
}