forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutationCount.java
38 lines (33 loc) · 1.03 KB
/
PermutationCount.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
package bigo;
public class PermutationCount {
public static void main(String[] args) {
String str = "abc";
permutation(str, 0, str.length() - 1);
}
private static void permutation(String str, String prefix) {
if (str.length() == 0) {
System.out.println(prefix);
} else {
for (int i = 0; i < str.length(); i++) {
String remainder = str.substring(0, i) + str.substring(i + 1);
permutation(remainder, prefix + str.charAt(i));
}
}
}
private static void permutation(String str, int l, int r) {
if (l == r) {
System.out.println(str);
}
for (int i = l; i <= r; i++) {
String swapped = swap(str, l, i);
permutation(swapped, l + 1, r);
}
}
private static String swap(String str, int i, int j) {
char[] arr = str.toCharArray();
char temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
return String.valueOf(arr);
}
}