-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutationSwap.java
More file actions
36 lines (31 loc) · 847 Bytes
/
Copy pathPermutationSwap.java
File metadata and controls
36 lines (31 loc) · 847 Bytes
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
public class PermutationSwap {
public static void swap(char arr[], int i, int j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void permutation (char arr[], int depth) {
if (depth == arr.length) {
for (char ch : arr)
System.out.print(ch + " ");
System.out.println();
return;
}
for (int i = depth; i < arr.length; i++) {
swap(arr, depth, i);
permutation(arr, depth + 1);
swap(arr, depth, i);
}
}
public static void main(String[] args) {
char characterArr[] = "ABC".toCharArray();
permutation(characterArr, 0);
// Output:
// A B C
// A C B
// B A C
// B C A
// C B A
// C A B
}
}