Skip to content

Commit 9509e4d

Browse files
Created a Permutation Function in the Recursion package. Closed the issue #7322
1 parent 2616e09 commit 9509e4d

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.thealgorithms.recursion;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* The Permutations class provides utility methods to generate all possible
11+
* rearrangements of elements for both integer arrays and strings.
12+
*
13+
* <p>This implementation uses a recursive approach and a {@link HashSet}
14+
* internally to ensure that only unique permutations are returned.</p>
15+
*/
16+
public final class Permutations {
17+
18+
private Permutations() {
19+
throw new UnsupportedOperationException("Utility Class");
20+
}
21+
22+
/**
23+
* Generates all unique permutations of an integer array.
24+
*
25+
* @param nums The array of integers to permute.
26+
* @return A List of Lists, where each inner list is a unique permutation.
27+
* @throws NullPointerException if nums is null.
28+
*/
29+
public static List<List<Integer>> permutation(int[] nums) {
30+
if (nums == null) throw new NullPointerException("Input array cannot be null");
31+
32+
List<Integer> up = Arrays.stream(nums).boxed().collect(Collectors.toList());
33+
HashSet<List<Integer>> set = generatePermutations(new ArrayList<>(), up);
34+
return new ArrayList<>(set);
35+
}
36+
37+
/**
38+
* Internal recursive helper to build permutations for integer lists.
39+
*/
40+
private static HashSet<List<Integer>> generatePermutations(List<Integer> p, List<Integer> up) {
41+
HashSet<List<Integer>> result = new HashSet<>();
42+
if (up.isEmpty()) {
43+
result.add(new ArrayList<>(p));
44+
return result;
45+
}
46+
47+
Integer num = up.get(0);
48+
List<Integer> remaining = up.subList(1, up.size());
49+
50+
for (int i = 0; i <= p.size(); i++) {
51+
List<Integer> nextP = new ArrayList<>(p);
52+
nextP.add(i, num); // Insert num at every possible position
53+
result.addAll(generatePermutations(nextP, remaining));
54+
}
55+
56+
return result;
57+
}
58+
59+
/**
60+
* Generates all possible permutations of a given string.
61+
*
62+
* @param s The string to permute.
63+
* @return A List containing all permutations of the input string.
64+
* @throws NullPointerException if s is null.
65+
*/
66+
public static List<String> permutation(String s) {
67+
if (s == null) throw new NullPointerException("Input string cannot be null");
68+
return generateStringPermutations("", s);
69+
}
70+
71+
/**
72+
* Internal recursive helper to build permutations for strings.
73+
*/
74+
private static List<String> generateStringPermutations(String p, String up) {
75+
List<String> list = new ArrayList<>();
76+
if (up.isEmpty()) {
77+
list.add(p);
78+
return list;
79+
}
80+
81+
char ch = up.charAt(0);
82+
String remaining = up.substring(1);
83+
84+
for (int i = 0; i <= p.length(); i++) {
85+
String first = p.substring(0, i);
86+
String second = p.substring(i);
87+
list.addAll(generateStringPermutations(first + ch + second, remaining));
88+
}
89+
return list;
90+
}
91+
}

0 commit comments

Comments
 (0)