forked from phishman3579/java-algorithms-implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutations.java
108 lines (93 loc) · 3.85 KB
/
Permutations.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.jwetherell.algorithms.mathematics;
import java.util.LinkedList;
import java.util.List;
/**
* In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence
* or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting.
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Permutation">Permutation (Wikipedia)</a>
* <br>
* @author Justin Wetherell <[email protected]>
* @author Lucjan Roslanowski <[email protected]>
*/
public class Permutations {
private Permutations() { }
/**
* N! permutation of the characters in the string (in order)
*/
public static String[] permutations(String stringToGeneratePermutationsFrom) {
final int size = numberOfPermutations(stringToGeneratePermutationsFrom.length());
final String[] list = new String[size];
final char[] prefix = new char[0];
final char[] chars = stringToGeneratePermutationsFrom.toCharArray();
permutations(list, 0, prefix, chars, 0, chars.length);
return list;
}
private static final int numberOfPermutations(int N) {
// factorial
int result = N;
while (N > 1)
result *= --N;
return result;
}
private static final int permutations(String[] list, int index, char[] prefix, char[] remaining, int prefixLength, int remainingLength) {
final int N = remainingLength-prefixLength;
if (N == 0) {
list[index]=new String(prefix);
index++;
} else {
for (int i=0; i<N; i++) {
final char[] prefChars = new char[prefixLength+1];
System.arraycopy(prefix, 0, prefChars, 0, prefixLength);
System.arraycopy(remaining, i, prefChars, prefixLength, 1);
final char[] restChars = new char[N-1];
System.arraycopy(remaining, 0, restChars, 0, i);
System.arraycopy(remaining, i+1, restChars, i, N-(i+1));
index = permutations(list, index, prefChars, restChars, remainingLength-(N-1), remainingLength);
}
}
return index;
}
/**
* Permutations of numbers in an array using recursion
* <br>
* int numbers[] = {7,5,3};
* LinkedList<LinkedList<Integer>> result = getAllPermutations(numbers);
*/
public static final <N extends Number> List<List<N>> getAllPermutations(final N[] numbers){
final List<List<N>> result = new LinkedList<List<N>>();
return getAllPermutations(numbers, result);
}
private static final <N extends Number> List<List<N>> getAllPermutations(final N[] numbers, List<List<N>> result){
//numbers given in an array are also a permutation
LinkedList<N> firstPermutation = new LinkedList<N>();
for (N el : numbers)
firstPermutation.add(el);
result.add(firstPermutation);
//let's permute all elements in array starting from index 0
return permute(numbers, 0, result);
}
private static final <N extends Number> List<List<N>> permute(final N[] numbers, int currentElementIndex, List<List<N>> result){
if(currentElementIndex == numbers.length - 1)
return result;
for(int i = currentElementIndex; i < numbers.length; ++i){
//swapping two elements
N temp = numbers[i];
numbers[i] = numbers[currentElementIndex];
numbers[currentElementIndex] = temp;
permute(numbers, currentElementIndex + 1,result);
//all next permutation found
if(i != currentElementIndex){
LinkedList<N> nextPermutation = new LinkedList<N>();
for(int j = 0; j < numbers.length; j++)
nextPermutation.add(numbers[j]);
result.add(nextPermutation);
}
//swapping back two elements
temp = numbers[i];
numbers[i] = numbers[currentElementIndex];
numbers[currentElementIndex] = temp;
}
return result;
}
}