Skip to content

Commit e7498fb

Browse files
committed
Added UniquePermutations algo with test cases
1 parent 7e33f9e commit e7498fb

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

src/main/java/com/thealgorithms/Recursion/UniquePermutations.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package com.thealgorithms.Recursion;
22

3-
public class UniquePermutations {
4-
throw new UnsupportedOperationException("Utility class");
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
56

7+
public class UniquePermutations {
68

79
public static List<String> getUniquePermutations(String str) {
8-
List<String> result = new ArrayList<>();
9-
boolean[] used = new boolean[str.length()];
10+
// Handle null or empty input
11+
if (str == null) return new ArrayList<>();
12+
if (str.length() == 0) {
13+
List<String> result = new ArrayList<>();
14+
result.add("");
15+
return result;
16+
}
17+
18+
// Sort characters to handle duplicates
1019
char[] chars = str.toCharArray();
20+
Arrays.sort(chars);
21+
22+
List<String> result = new ArrayList<>();
23+
boolean[] used = new boolean[chars.length];
1124
StringBuilder currentPermutation = new StringBuilder();
1225
generatePermutations(chars, used, currentPermutation, result);
1326
return result;
@@ -20,6 +33,7 @@ private static void generatePermutations(char[] chars, boolean[] used, StringBui
2033
}
2134

2235
for (int i = 0; i < chars.length; i++) {
36+
// Skip used characters or duplicates
2337
if (used[i] || (i > 0 && chars[i] == chars[i - 1] && !used[i - 1])) {
2438
continue;
2539
}
@@ -32,3 +46,7 @@ private static void generatePermutations(char[] chars, boolean[] used, StringBui
3246
}
3347
}
3448

49+
50+
//This is a more efficient but complex algorithm
51+
//If you want to refer to a simpler one and then come to this,
52+
//click on the URL=>"https://www.geeksforgeeks.org/java-program-to-print-distinct-permutations-of-a-string/""

src/test/java/com/thealgorithms/Recursion/UniquePermutationsTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package com.thealgorithms.recursion;
1+
package com.thealgorithms.Recursion;
22

33
import org.junit.jupiter.api.Test;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
46
import java.util.List;
57
import static org.junit.jupiter.api.Assertions.assertEquals;
68

@@ -11,7 +13,8 @@ void testUniquePermutations() {
1113
String str = "abc";
1214
List<String> expected = List.of("abc", "acb", "bac", "bca", "cab", "cba");
1315
List<String> result = UniquePermutations.getUniquePermutations(str);
14-
assertEquals(expected.size(), result.size());
16+
Collections.sort(expected); // Ensure expected is sorted
17+
Collections.sort(result); // Ensure result is sorted
1518
assertEquals(expected, result);
1619
}
1720

@@ -20,7 +23,8 @@ void testUniquePermutationsWithDuplicates() {
2023
String str = "aab";
2124
List<String> expected = List.of("aab", "aba", "baa");
2225
List<String> result = UniquePermutations.getUniquePermutations(str);
23-
assertEquals(expected.size(), result.size());
26+
Collections.sort(expected); // Ensure expected is sorted
27+
Collections.sort(result); // Ensure result is sorted
2428
assertEquals(expected, result);
2529
}
2630

@@ -29,6 +33,7 @@ void testEmptyString() {
2933
String str = "";
3034
List<String> result = UniquePermutations.getUniquePermutations(str);
3135
assertEquals(1, result.size()); // only 1 permutation which is an empty string
36+
assertEquals("", result.get(0)); // Verify the only permutation is the empty string
3237
}
3338

3439
@Test
@@ -38,4 +43,30 @@ void testSingleCharacter() {
3843
assertEquals(1, result.size());
3944
assertEquals("a", result.get(0));
4045
}
46+
47+
@Test
48+
void testAllIdenticalCharacters() {
49+
String str = "aaa";
50+
List<String> expected = List.of("aaa");
51+
List<String> result = UniquePermutations.getUniquePermutations(str);
52+
assertEquals(expected.size(), result.size());
53+
assertEquals(expected, result);
54+
}
55+
56+
@Test
57+
void testMixedCaseCharacters() {
58+
String str = "aAb";
59+
List<String> expected = List.of("AaB", "AbA", "aAB", "aBA", "baA", "bAa");
60+
List<String> result = UniquePermutations.getUniquePermutations(str);
61+
Collections.sort(expected); // Ensure expected is sorted
62+
Collections.sort(result); // Ensure result is sorted
63+
assertEquals(expected, result);
64+
}
65+
66+
@Test
67+
void testNullInput() {
68+
String str = null;
69+
List<String> result = UniquePermutations.getUniquePermutations(str);
70+
assertEquals(0, result.size()); // Expect an empty list for null input
71+
}
4172
}

0 commit comments

Comments
 (0)