-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutation.java
38 lines (34 loc) · 1.01 KB
/
Permutation.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
public class Permutation {
public static void permutation(String str, boolean visited[], char characters[], int bound, int depth){
if (depth == bound){
for (int i = 0; i < bound; i++) {
System.out.print(characters[i]);
}
System.out.println();
return;
}
for (int i = 0; i < str.length(); i++) {
if (visited[i] != true){
visited[i] = true;
characters[depth] = str.charAt(i);
permutation(str, visited, characters, bound, depth + 1);
visited[i] = false;
}
}
}
public static void main(String[] args) {
String str = "abcd";
boolean visited[] = new boolean[str.length()];
char characters[] = new char[str.length()];
int r = 2;
/* print
ab
ac
ba
bc
ca
cb
*/
permutation(str, visited, characters, r, 0);
}
}