-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateParenthesis.java
More file actions
36 lines (31 loc) · 1.1 KB
/
Copy pathgenerateParenthesis.java
File metadata and controls
36 lines (31 loc) · 1.1 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
import java.util.ArrayList;
import java.util.List;
public class generateParenthesis {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
List<String> output = new ArrayList<>();
backtrack(n, 0, 0, output, result);
return result;
}
public static void backtrack(int n, int leftCount, int rightCount, List<String> output, List<String> result) {
if (leftCount >= n && rightCount >= n) {
String outputStr = String.join("", output);
result.add(outputStr);
}
if (leftCount < n) {
output.add("(");
backtrack(n, leftCount + 1, rightCount, output, result);
output.remove(output.size() - 1);
}
if (rightCount < leftCount) {
output.add(")");
backtrack(n, leftCount, rightCount + 1, output, result);
output.remove(output.size() - 1);
}
}
public static void main(String[] args) {
System.out.println(generateParenthesis(3));
}
}
// time complexity-O(2^2n)
// space complexity-O(n)