- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathPermutations.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
packagecom.jwetherell.algorithms.mathematics;
importjava.util.LinkedList;
importjava.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 <phishman3579@gmail.com>
* @author Lucjan Roslanowski <lucjanroslanowski@gmail.com>
*/
publicclassPermutations {
privatePermutations() { }
/**
* N! permutation of the characters in the string (in order)
*/
publicstaticString[] permutations(StringstringToGeneratePermutationsFrom) {
finalintsize = numberOfPermutations(stringToGeneratePermutationsFrom.length());
finalString[] list = newString[size];
finalchar[] prefix = newchar[0];
finalchar[] chars = stringToGeneratePermutationsFrom.toCharArray();
permutations(list, 0, prefix, chars, 0, chars.length);
returnlist;
}
privatestaticfinalintnumberOfPermutations(intN) {
// factorial
intresult = N;
while (N > 1)
result *= --N;
returnresult;
}
privatestaticfinalintpermutations(String[] list, intindex, char[] prefix, char[] remaining, intprefixLength, intremainingLength) {
finalintN = remainingLength-prefixLength;
if (N == 0) {
list[index]=newString(prefix);
index++;
} else {
for (inti=0; i<N; i++) {
finalchar[] prefChars = newchar[prefixLength+1];
System.arraycopy(prefix, 0, prefChars, 0, prefixLength);
System.arraycopy(remaining, i, prefChars, prefixLength, 1);
finalchar[] restChars = newchar[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);
}
}
returnindex;
}
/**
* Permutations of numbers in an array using recursion
* <br>
* int numbers[] = {7,5,3};
* LinkedList<LinkedList<Integer>> result = getAllPermutations(numbers);
*/
publicstaticfinal <NextendsNumber> List<List<N>> getAllPermutations(finalN[] numbers){
finalList<List<N>> result = newLinkedList<List<N>>();
returngetAllPermutations(numbers, result);
}
privatestaticfinal <NextendsNumber> List<List<N>> getAllPermutations(finalN[] numbers, List<List<N>> result){
//numbers given in an array are also a permutation
LinkedList<N> firstPermutation = newLinkedList<N>();
for (Nel : numbers)
firstPermutation.add(el);
result.add(firstPermutation);
//let's permute all elements in array starting from index 0
returnpermute(numbers, 0, result);
}
privatestaticfinal <NextendsNumber> List<List<N>> permute(finalN[] numbers, intcurrentElementIndex, List<List<N>> result){
if(currentElementIndex == numbers.length - 1)
returnresult;
for(inti = currentElementIndex; i < numbers.length; ++i){
//swapping two elements
Ntemp = numbers[i];
numbers[i] = numbers[currentElementIndex];
numbers[currentElementIndex] = temp;
permute(numbers, currentElementIndex + 1,result);
//all next permutation found
if(i != currentElementIndex){
LinkedList<N> nextPermutation = newLinkedList<N>();
for(intj = 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;
}
returnresult;
}
}