-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathPermutations.java
More file actions
84 lines (76 loc) · 2.5 KB
/
Permutations.java
File metadata and controls
84 lines (76 loc) · 2.5 KB
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
package com.thealgorithms.recursion;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* This class provides methods to generate all permutations
* of a given array of any type using recursion.
* <p>
* Reference:
* https://en.wikipedia.org/wiki/Permutation
*/
public final class Permutations {
private Permutations() {
throw new UnsupportedOperationException("Utility class");
}
/**
* Generates all permutations of a generic array.
*
* @param <T> the type of elements in the array
* @param items the input array
* @return list of all permutations
* @throws NullPointerException if items is null
* @throws IllegalArgumentException if any element in items is null
*/
public static <T> List<List<T>> permutations(T[] items) {
if (items == null) {
throw new NullPointerException("Input array cannot be null");
}
for (T item : items) {
if (item == null) {
throw new IllegalArgumentException("Array elements cannot be null");
}
}
List<List<T>> result = new ArrayList<>();
List<T> list = new ArrayList<>(Arrays.asList(items));
generatePermutations(0, list, result);
return result;
}
/**
* Recursive backtracking to generate permutations.
*
* @param <T> the type of elements
* @param index the current position being fixed
* @param list the working list of elements
* @param result the accumulated list of permutations
*/
private static <T> void generatePermutations(int index, List<T> list, List<List<T>> result) {
if (index == list.size()) {
result.add(new ArrayList<>(list));
return;
}
Set<T> seen = new HashSet<>();
for (int i = index; i < list.size(); i++) {
if (seen.add(list.get(i))) { // skip duplicate values at this position
swap(list, index, i);
generatePermutations(index + 1, list, result);
swap(list, index, i); // backtrack
}
}
}
/**
* Swaps two elements in a list.
*
* @param <T> the type of elements
* @param list the list
* @param i first index
* @param j second index
*/
private static <T> void swap(List<T> list, int i, int j) {
T temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}