-
Notifications
You must be signed in to change notification settings - Fork 1
/
AllValidPermutationsOfParenthesesIII.java
72 lines (59 loc) · 2.24 KB
/
AllValidPermutationsOfParenthesesIII.java
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
60
61
62
63
64
65
66
67
68
69
70
71
/*
Get all valid permutations of l pairs of (), m pairs of <> and n pairs of {},
subject to the priority restriction: {} higher than <> higher than ().
Assumptions
l, m, n >= 0
l + m + n >= 0
Examples
l = 1, m = 1, n = 0, all the valid permutations are ["()<>", "<()>", "<>()"].
l = 2, m = 0, n = 1, all the valid permutations are [“()(){}”, “(){()}”, “(){}()”, “{()()}”, “{()}()”, “{}()()”].
*/
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class AllValidPermutationsOfParenthesesIII {
char[] p = new char[] {'(', ')', '<', '>', '{', '}'}; // pick array
int[] c; // count for our picks
public List<String> validParenthesesIII(int l, int m, int n) {
List<String> res = new ArrayList<>();
int len = 2 * (l + m + n);
c = new int[]{l, l, m, m, n, n};
char[] cur = new char[len];
Deque<Integer> stack = new ArrayDeque<>();
dfs(0, stack, cur, res);
return res;
}
private void dfs(int idx, Deque<Integer> stack, char[] cur, List<String> res) {
if (idx == cur.length) {
res.add(new String(cur));
return;
}
for (int i = 0; i < c.length; i++)
if (i % 2 == 0) {
if (c[i] > 0 && (stack.isEmpty() || i <= stack.peekFirst())) {
stack.offerFirst(i);
add(idx, i, cur);
dfs(idx + 1, stack, cur, res);
stack.pollFirst();
c[i]++;
}
} else {
if (!stack.isEmpty() && stack.peekFirst() == i - 1) {
stack.pollFirst();
add(idx, i, cur);
dfs(idx + 1, stack, cur, res);
stack.offerFirst(i-1);
c[i]++;
}
}
}
private void add(int idx, int i, char[] cur) {
c[i]--;
cur[idx] = p[i];
}
public static void main(String[] args) {
AllValidPermutationsOfParenthesesIII avpop3 = new AllValidPermutationsOfParenthesesIII();
System.out.println(avpop3.validParenthesesIII(3, 1, 0));
}
}