forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllpermutationofparanthesis.java
More file actions
37 lines (34 loc) · 1.01 KB
/
Allpermutationofparanthesis.java
File metadata and controls
37 lines (34 loc) · 1.01 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
import java.io.*;
class Allpermutationofparanthesis {
static void _printParenthesis(char str[], int pos,
int n, int open,
int close) {
if (close == n) {
for (int i = 0; i < str.length; i++)
System.out.print(str[i]);
System.out.println();
return;
} else {
if (open > close) {
str[pos] = '}';
_printParenthesis(str, pos + 1, n, open,
close + 1);
}
if (open < n) {
str[pos] = '{';
_printParenthesis(str, pos + 1, n, open + 1,
close);
}
}
}
static void printParenthesis(char str[], int n) {
if (n > 0)
_printParenthesis(str, 0, n, 0, 0);
return;
}
public static void main(String[] args) {
int n = 3;
char[] str = new char[2 * n];
printParenthesis(str, n);
}
}