-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesisPermutation.java
More file actions
65 lines (53 loc) · 1.75 KB
/
ParenthesisPermutation.java
File metadata and controls
65 lines (53 loc) · 1.75 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
import java.util.ArrayList;
class ParenthesisPermutation {
public static void main(String[] args) {
int[] tests = {1,2,3,4,5};
for(int num : tests){
thePerms(num);
}
}
public static void thePerms(int n){
if(n == 0){
System.out.println("");
System.out.println("-----------------------");
return;
}
if(n == 1){
System.out.println("()");
System.out.println("-----------------------");
return;
}
ArrayList<StringBuilder> perms = new ArrayList<>();
perms.add(new StringBuilder("()"));
int longest = 0;
int start = 0;
for(int i = 0; i < perms.size(); i++){
StringBuilder curr = perms.get(i);
if(curr.length() == n * 2){
break;
}
for(int j = 0; j < curr.length(); j++){
if(j + 1 < curr.length()){
String theSub = curr.substring(j, j+2);
if(theSub.compareTo("))") == 0){
j++;
continue;
}
if(theSub.compareTo(")(") == 0){
continue;
}
}
StringBuilder per = new StringBuilder(curr.substring(0,j+1)).append("()").append(curr.substring(j+1));
perms.add(per);
if(per.length() > longest){
start = perms.size() - 1;
longest = per.length();
}
}
}
while(start < perms.size()){
System.out.println(perms.get(start++));
}
System.out.println("-----------------------");
}
}