-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllValidPermutationsOfParenthesesI.java
More file actions
99 lines (84 loc) · 2.71 KB
/
AllValidPermutationsOfParenthesesI.java
File metadata and controls
99 lines (84 loc) · 2.71 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Given N pairs of parentheses “()”, return a list with all the valid permutations.
Assumptions
N > 0
Examples
N = 1, all valid permutations are ["()"]
N = 3, all valid permutations are ["((()))", "(()())", "(())()", "()(())", "()()()"]
*/
import java.util.ArrayList;
import java.util.List;
public class AllValidPermutationsOfParenthesesI {
// Solution using StringBuilder starts here
public List<String> validParentheses(int n) {
StringBuilder sb = new StringBuilder();
List<String> res = new ArrayList<>();
dfs(n, 0, 0, sb, res);
return res;
}
private void dfs(int n, int l, int r, StringBuilder sb, List<String> res) {
if (l == n && r == n) {
res.add(sb.toString());
}
if (l < n) {
dfs(n, l + 1, r, sb.append('('), res);
sb.deleteCharAt(sb.length() - 1);
}
if (r < l) {
dfs(n, l, r + 1, sb.append(')'), res);
sb.deleteCharAt(sb.length() - 1);
}
}
// Solution using StringBuilder ends here
// for print out if blocks
public void printBlocks(List<String> res) {
for (String s : res) {
printBlock(s);
System.out.println();
}
}
public void printBlock(String s) {
int spaces = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == '(') {
printSpaces(spaces);
System.out.print("if {\n");
spaces += 2;
} else {
printSpaces(spaces -= 2);
System.out.print("}\n");
}
}
}
public void printSpaces(int e) {
System.out.print(" ".repeat(e));
}
// print if blocks logic ends here
// Solution using char array starts here
public List<String> usingCharArray(int n) { // TC: O(2^2n), SC: O(2n)
List<String> res = new ArrayList<>();
char[] cur = new char[2 * n];
usingCharArray(0, 0, cur, res);
return res;
}
public void usingCharArray(int l, int r, char[] cur, List<String> res) {
if (l + r == cur.length) {
res.add(new String(cur));
return;
}
if (l < cur.length / 2) {
cur[l + r] = '(';
usingCharArray(l + 1, r, cur, res);
}
if (r < l) {
cur[l + r] = ')';
usingCharArray(l, r + 1, cur, res);
}
}
// Solution use char array ends here
public static void main(String[] args) {
AllValidPermutationsOfParenthesesI avpop = new AllValidPermutationsOfParenthesesI();
avpop.printBlocks(avpop.validParentheses(3));
}
}