From 9509e4d339715661f8d8bf9f423468aaa5ed92ed Mon Sep 17 00:00:00 2001 From: Sumanyu Rajput Date: Mon, 4 May 2026 22:42:06 +0530 Subject: [PATCH 1/8] Created a Permutation Function in the Recursion package. Closed the issue #7322 --- .../thealgorithms/recursion/Permutations.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/main/java/com/thealgorithms/recursion/Permutations.java diff --git a/src/main/java/com/thealgorithms/recursion/Permutations.java b/src/main/java/com/thealgorithms/recursion/Permutations.java new file mode 100644 index 000000000000..1ea339d4e941 --- /dev/null +++ b/src/main/java/com/thealgorithms/recursion/Permutations.java @@ -0,0 +1,91 @@ +package com.thealgorithms.recursion; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +/** + * The Permutations class provides utility methods to generate all possible + * rearrangements of elements for both integer arrays and strings. + * + *

This implementation uses a recursive approach and a {@link HashSet} + * internally to ensure that only unique permutations are returned.

+ */ +public final class Permutations { + + private Permutations() { + throw new UnsupportedOperationException("Utility Class"); + } + + /** + * Generates all unique permutations of an integer array. + * + * @param nums The array of integers to permute. + * @return A List of Lists, where each inner list is a unique permutation. + * @throws NullPointerException if nums is null. + */ + public static List> permutation(int[] nums) { + if (nums == null) throw new NullPointerException("Input array cannot be null"); + + List up = Arrays.stream(nums).boxed().collect(Collectors.toList()); + HashSet> set = generatePermutations(new ArrayList<>(), up); + return new ArrayList<>(set); + } + + /** + * Internal recursive helper to build permutations for integer lists. + */ + private static HashSet> generatePermutations(List p, List up) { + HashSet> result = new HashSet<>(); + if (up.isEmpty()) { + result.add(new ArrayList<>(p)); + return result; + } + + Integer num = up.get(0); + List remaining = up.subList(1, up.size()); + + for (int i = 0; i <= p.size(); i++) { + List nextP = new ArrayList<>(p); + nextP.add(i, num); // Insert num at every possible position + result.addAll(generatePermutations(nextP, remaining)); + } + + return result; + } + + /** + * Generates all possible permutations of a given string. + * + * @param s The string to permute. + * @return A List containing all permutations of the input string. + * @throws NullPointerException if s is null. + */ + public static List permutation(String s) { + if (s == null) throw new NullPointerException("Input string cannot be null"); + return generateStringPermutations("", s); + } + + /** + * Internal recursive helper to build permutations for strings. + */ + private static List generateStringPermutations(String p, String up) { + List list = new ArrayList<>(); + if (up.isEmpty()) { + list.add(p); + return list; + } + + char ch = up.charAt(0); + String remaining = up.substring(1); + + for (int i = 0; i <= p.length(); i++) { + String first = p.substring(0, i); + String second = p.substring(i); + list.addAll(generateStringPermutations(first + ch + second, remaining)); + } + return list; + } +} From fdf5fd16044d7c0aa313283f09bd46254029e985 Mon Sep 17 00:00:00 2001 From: Sumanyu Rajput Date: Mon, 4 May 2026 23:03:13 +0530 Subject: [PATCH 2/8] Refactor permutations implementation, fix formatting, add tests --- .../thealgorithms/recursion/Permutations.java | 125 ++++++++++-------- .../thealgorithms/recursion/Permutations.java | 22 +++ 2 files changed, 89 insertions(+), 58 deletions(-) create mode 100644 src/test/java/com/thealgorithms/recursion/Permutations.java diff --git a/src/main/java/com/thealgorithms/recursion/Permutations.java b/src/main/java/com/thealgorithms/recursion/Permutations.java index 1ea339d4e941..3e0e1552f74e 100644 --- a/src/main/java/com/thealgorithms/recursion/Permutations.java +++ b/src/main/java/com/thealgorithms/recursion/Permutations.java @@ -2,90 +2,99 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import java.util.stream.Collectors; /** - * The Permutations class provides utility methods to generate all possible - * rearrangements of elements for both integer arrays and strings. - * - *

This implementation uses a recursive approach and a {@link HashSet} - * internally to ensure that only unique permutations are returned.

+ * This class provides methods to generate all permutations + * of a given integer array or string using recursion. + * + * Reference: + * https://en.wikipedia.org/wiki/Permutation */ public final class Permutations { private Permutations() { - throw new UnsupportedOperationException("Utility Class"); + throw new UnsupportedOperationException("Utility class"); } /** - * Generates all unique permutations of an integer array. + * Generates all permutations of an integer array. * - * @param nums The array of integers to permute. - * @return A List of Lists, where each inner list is a unique permutation. - * @throws NullPointerException if nums is null. + * @param nums the input array + * @return list of all permutations + * @throws NullPointerException if nums is null */ - public static List> permutation(int[] nums) { - if (nums == null) throw new NullPointerException("Input array cannot be null"); - - List up = Arrays.stream(nums).boxed().collect(Collectors.toList()); - HashSet> set = generatePermutations(new ArrayList<>(), up); - return new ArrayList<>(set); - } - - /** - * Internal recursive helper to build permutations for integer lists. - */ - private static HashSet> generatePermutations(List p, List up) { - HashSet> result = new HashSet<>(); - if (up.isEmpty()) { - result.add(new ArrayList<>(p)); - return result; + public static List> permutations(int[] nums) { + if (nums == null) { + throw new NullPointerException("Input array cannot be null"); } - Integer num = up.get(0); - List remaining = up.subList(1, up.size()); + List> result = new ArrayList<>(); + List list = new ArrayList<>(); - for (int i = 0; i <= p.size(); i++) { - List nextP = new ArrayList<>(p); - nextP.add(i, num); // Insert num at every possible position - result.addAll(generatePermutations(nextP, remaining)); + for (int num : nums) { + list.add(num); } + generateIntegerPermutations(0, list, result); return result; } - /** - * Generates all possible permutations of a given string. - * - * @param s The string to permute. - * @return A List containing all permutations of the input string. - * @throws NullPointerException if s is null. - */ - public static List permutation(String s) { - if (s == null) throw new NullPointerException("Input string cannot be null"); - return generateStringPermutations("", s); + private static void generateIntegerPermutations( + int index, + List list, + List> result + ) { + if (index == list.size()) { + result.add(new ArrayList<>(list)); + return; + } + + for (int i = index; i < list.size(); i++) { + swap(list, index, i); + generateIntegerPermutations(index + 1, list, result); + swap(list, index, i); // backtrack + } + } + + private static void swap(List list, int i, int j) { + Integer temp = list.get(i); + list.set(i, list.get(j)); + list.set(j, temp); } /** - * Internal recursive helper to build permutations for strings. + * Generates all permutations of a string. + * + * @param s the input string + * @return list of all permutations + * @throws NullPointerException if s is null */ - private static List generateStringPermutations(String p, String up) { - List list = new ArrayList<>(); - if (up.isEmpty()) { - list.add(p); - return list; + public static List permutations(String s) { + if (s == null) { + throw new NullPointerException("Input string cannot be null"); } - char ch = up.charAt(0); - String remaining = up.substring(1); + List result = new ArrayList<>(); + generateStringPermutations("", s, result); + return result; + } + + private static void generateStringPermutations( + String prefix, + String remaining, + List result + ) { + if (remaining.isEmpty()) { + result.add(prefix); + return; + } - for (int i = 0; i <= p.length(); i++) { - String first = p.substring(0, i); - String second = p.substring(i); - list.addAll(generateStringPermutations(first + ch + second, remaining)); + for (int i = 0; i < remaining.length(); i++) { + char ch = remaining.charAt(i); + String next = + remaining.substring(0, i) + remaining.substring(i + 1); + generateStringPermutations(prefix + ch, next, result); } - return list; } -} +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/recursion/Permutations.java b/src/test/java/com/thealgorithms/recursion/Permutations.java new file mode 100644 index 000000000000..7d7cc6447855 --- /dev/null +++ b/src/test/java/com/thealgorithms/recursion/Permutations.java @@ -0,0 +1,22 @@ +package com.thealgorithms.recursion; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class PermutationsTest { + + @Test + void testIntegerPermutations() { + int[] nums = {1, 2}; + List> result = Permutations.permutations(nums); + assertEquals(2, result.size()); + } + + @Test + void testStringPermutations() { + List result = Permutations.permutations("ab"); + assertEquals(2, result.size()); + } +} \ No newline at end of file From 0c53a721504fc9d069074689db68131c6db82ca9 Mon Sep 17 00:00:00 2001 From: Sumanyu Rajput Date: Mon, 4 May 2026 23:21:08 +0530 Subject: [PATCH 3/8] Fix Checkstyle errors and improve test coverage in Permutations --- .../thealgorithms/recursion/Permutations.java | 18 ++++-------------- .../thealgorithms/recursion/Permutations.java | 13 ++++++++++++- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Permutations.java b/src/main/java/com/thealgorithms/recursion/Permutations.java index 3e0e1552f74e..d886c81186cd 100644 --- a/src/main/java/com/thealgorithms/recursion/Permutations.java +++ b/src/main/java/com/thealgorithms/recursion/Permutations.java @@ -1,7 +1,6 @@ package com.thealgorithms.recursion; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** @@ -40,11 +39,7 @@ public static List> permutations(int[] nums) { return result; } - private static void generateIntegerPermutations( - int index, - List list, - List> result - ) { + private static void generateIntegerPermutations(int index, List list, List> result) { if (index == list.size()) { result.add(new ArrayList<>(list)); return; @@ -80,11 +75,7 @@ public static List permutations(String s) { return result; } - private static void generateStringPermutations( - String prefix, - String remaining, - List result - ) { + private static void generateStringPermutations(String prefix, String remaining, List result) { if (remaining.isEmpty()) { result.add(prefix); return; @@ -92,9 +83,8 @@ private static void generateStringPermutations( for (int i = 0; i < remaining.length(); i++) { char ch = remaining.charAt(i); - String next = - remaining.substring(0, i) + remaining.substring(i + 1); + String next = remaining.substring(0, i) + remaining.substring(i + 1); generateStringPermutations(prefix + ch, next, result); } } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/recursion/Permutations.java b/src/test/java/com/thealgorithms/recursion/Permutations.java index 7d7cc6447855..a236f4963385 100644 --- a/src/test/java/com/thealgorithms/recursion/Permutations.java +++ b/src/test/java/com/thealgorithms/recursion/Permutations.java @@ -1,6 +1,7 @@ package com.thealgorithms.recursion; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; import org.junit.jupiter.api.Test; @@ -14,9 +15,19 @@ void testIntegerPermutations() { assertEquals(2, result.size()); } + @Test + void testIntegerPermutationsNull() { + assertThrows(NullPointerException.class, () -> Permutations.permutations((int[]) null)); + } + @Test void testStringPermutations() { List result = Permutations.permutations("ab"); assertEquals(2, result.size()); } -} \ No newline at end of file + + @Test + void testStringPermutationsNull() { + assertThrows(NullPointerException.class, () -> Permutations.permutations((String) null)); + } +} From a5b38644cea1d79faf5d5b6d59c52c798f5c26b4 Mon Sep 17 00:00:00 2001 From: trynafind-sumanyu Date: Tue, 5 May 2026 18:14:21 +0530 Subject: [PATCH 4/8] Fixed the duplicates and made it generic --- .../thealgorithms/recursion/Permutations.java | 90 +++++++++---------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Permutations.java b/src/main/java/com/thealgorithms/recursion/Permutations.java index d886c81186cd..6dda12db6957 100644 --- a/src/main/java/com/thealgorithms/recursion/Permutations.java +++ b/src/main/java/com/thealgorithms/recursion/Permutations.java @@ -1,11 +1,14 @@ 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 integer array or string using recursion. + * of a given array of any type using recursion. * * Reference: * https://en.wikipedia.org/wiki/Permutation @@ -17,74 +20,65 @@ private Permutations() { } /** - * Generates all permutations of an integer array. + * Generates all permutations of a generic array. * - * @param nums the input array + * @param the type of elements in the array + * @param items the input array * @return list of all permutations - * @throws NullPointerException if nums is null + * @throws NullPointerException if items is null + * @throws IllegalArgumentException if any element in items is null */ - public static List> permutations(int[] nums) { - if (nums == null) { + public static List> permutations(T[] items) { + if (items == null) { throw new NullPointerException("Input array cannot be null"); } - - List> result = new ArrayList<>(); - List list = new ArrayList<>(); - - for (int num : nums) { - list.add(num); + for (T item : items) { + if (item == null) { + throw new IllegalArgumentException("Array elements cannot be null"); + } } - generateIntegerPermutations(0, list, result); + List> result = new ArrayList<>(); + List list = new ArrayList<>(Arrays.asList(items)); + generatePermutations(0, list, result); return result; } - private static void generateIntegerPermutations(int index, List list, List> result) { + /** + * Recursive backtracking to generate permutations. + * + * @param 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 void generatePermutations(int index, List list, List> result) { if (index == list.size()) { result.add(new ArrayList<>(list)); return; } + Set seen = new HashSet<>(); for (int i = index; i < list.size(); i++) { - swap(list, index, i); - generateIntegerPermutations(index + 1, list, result); - swap(list, index, i); // backtrack + 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 + } } } - private static void swap(List list, int i, int j) { - Integer temp = list.get(i); - list.set(i, list.get(j)); - list.set(j, temp); - } - /** - * Generates all permutations of a string. + * Swaps two elements in a list. * - * @param s the input string - * @return list of all permutations - * @throws NullPointerException if s is null + * @param the type of elements + * @param list the list + * @param i first index + * @param j second index */ - public static List permutations(String s) { - if (s == null) { - throw new NullPointerException("Input string cannot be null"); - } - - List result = new ArrayList<>(); - generateStringPermutations("", s, result); - return result; - } - - private static void generateStringPermutations(String prefix, String remaining, List result) { - if (remaining.isEmpty()) { - result.add(prefix); - return; - } - - for (int i = 0; i < remaining.length(); i++) { - char ch = remaining.charAt(i); - String next = remaining.substring(0, i) + remaining.substring(i + 1); - generateStringPermutations(prefix + ch, next, result); - } + private static void swap(List list, int i, int j) { + T temp = list.get(i); + list.set(i, list.get(j)); + list.set(j, temp); } } From d9e43ba6e689a909b87faf59699d0cb71d0cebfc Mon Sep 17 00:00:00 2001 From: trynafind-sumanyu Date: Tue, 5 May 2026 18:17:29 +0530 Subject: [PATCH 5/8] Fixed test file too for the duplicates change --- .../thealgorithms/recursion/Permutations.java | 142 ++++++++++++++++-- 1 file changed, 133 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/thealgorithms/recursion/Permutations.java b/src/test/java/com/thealgorithms/recursion/Permutations.java index a236f4963385..63930e3ec3a8 100644 --- a/src/test/java/com/thealgorithms/recursion/Permutations.java +++ b/src/test/java/com/thealgorithms/recursion/Permutations.java @@ -2,32 +2,156 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import org.junit.jupiter.api.Test; class PermutationsTest { + // ───────────────────────────────────────────── + // Null / Invalid Input Tests + // ───────────────────────────────────────────── + + @Test + void testNullArrayThrowsNullPointerException() { + assertThrows(NullPointerException.class, () -> Permutations.permutations((Integer[]) null)); + } + + @Test + void testArrayWithNullElementThrowsIllegalArgumentException() { + Integer[] items = { 1, null, 3 }; + assertThrows(IllegalArgumentException.class, () -> Permutations.permutations(items)); + } + + // ───────────────────────────────────────────── + // Edge Case Tests + // ───────────────────────────────────────────── + + @Test + void testEmptyArrayReturnsOneEmptyPermutation() { + Integer[] items = {}; + List> result = Permutations.permutations(items); + assertEquals(1, result.size()); + assertTrue(result.get(0).isEmpty()); + } + + @Test + void testSingleElementReturnsOnePermutation() { + Integer[] items = { 42 }; + List> result = Permutations.permutations(items); + assertEquals(1, result.size()); + assertEquals(List.of(42), result.get(0)); + } + + // ───────────────────────────────────────────── + // Integer Permutation Tests + // ───────────────────────────────────────────── + @Test - void testIntegerPermutations() { - int[] nums = {1, 2}; - List> result = Permutations.permutations(nums); + void testTwoIntegersReturnsTwoPermutations() { + Integer[] items = { 1, 2 }; + List> result = Permutations.permutations(items); assertEquals(2, result.size()); + assertTrue(result.contains(List.of(1, 2))); + assertTrue(result.contains(List.of(2, 1))); } @Test - void testIntegerPermutationsNull() { - assertThrows(NullPointerException.class, () -> Permutations.permutations((int[]) null)); + void testThreeIntegersReturnsSixPermutations() { + Integer[] items = { 1, 2, 3 }; + List> result = Permutations.permutations(items); + assertEquals(6, result.size()); } @Test - void testStringPermutations() { - List result = Permutations.permutations("ab"); + void testIntegerPermutationsContainAllExpectedOrders() { + Integer[] items = { 1, 2, 3 }; + List> result = Permutations.permutations(items); + assertTrue(result.contains(List.of(1, 2, 3))); + assertTrue(result.contains(List.of(1, 3, 2))); + assertTrue(result.contains(List.of(2, 1, 3))); + assertTrue(result.contains(List.of(2, 3, 1))); + assertTrue(result.contains(List.of(3, 1, 2))); + assertTrue(result.contains(List.of(3, 2, 1))); + } + + // ───────────────────────────────────────────── + // Duplicate Handling Tests + // ───────────────────────────────────────────── + + @Test + void testTwoDuplicateIntegersReturnsOnePermutation() { + Integer[] items = { 1, 1 }; + List> result = Permutations.permutations(items); + assertEquals(1, result.size()); + assertEquals(List.of(1, 1), result.get(0)); + } + + @Test + void testArrayWithDuplicatesReturnsCorrectCount() { + Integer[] items = { 1, 1, 2 }; + List> result = Permutations.permutations(items); + // 3!/2! = 3 unique permutations + assertEquals(3, result.size()); + assertTrue(result.contains(List.of(1, 1, 2))); + assertTrue(result.contains(List.of(1, 2, 1))); + assertTrue(result.contains(List.of(2, 1, 1))); + } + + @Test + void testAllDuplicatesReturnsOnePermutation() { + Integer[] items = { 5, 5, 5 }; + List> result = Permutations.permutations(items); + assertEquals(1, result.size()); + assertEquals(List.of(5, 5, 5), result.get(0)); + } + + // ───────────────────────────────────────────── + // String Permutation Tests + // ───────────────────────────────────────────── + + @Test + void testTwoStringsReturnsTwoPermutations() { + String[] items = { "a", "b" }; + List> result = Permutations.permutations(items); assertEquals(2, result.size()); + assertTrue(result.contains(List.of("a", "b"))); + assertTrue(result.contains(List.of("b", "a"))); + } + + @Test + void testThreeStringsReturnsSixPermutations() { + String[] items = { "x", "y", "z" }; + List> result = Permutations.permutations(items); + assertEquals(6, result.size()); + } + + @Test + void testDuplicateStringsReturnsCorrectCount() { + String[] items = { "a", "a", "b" }; + List> result = Permutations.permutations(items); + assertEquals(3, result.size()); + assertTrue(result.contains(List.of("a", "a", "b"))); + assertTrue(result.contains(List.of("a", "b", "a"))); + assertTrue(result.contains(List.of("b", "a", "a"))); + } + + // ───────────────────────────────────────────── + // Character Permutation Tests + // ───────────────────────────────────────────── + + @Test + void testCharacterPermutations() { + Character[] items = { 'a', 'b', 'c' }; + List> result = Permutations.permutations(items); + assertEquals(6, result.size()); } @Test - void testStringPermutationsNull() { - assertThrows(NullPointerException.class, () -> Permutations.permutations((String) null)); + void testDuplicateCharactersReturnsCorrectCount() { + Character[] items = { 'a', 'a', 'b' }; + List> result = Permutations.permutations(items); + assertEquals(3, result.size()); } } From c1368b4fc41ea7b88f27bef7646dc1624c4cd500 Mon Sep 17 00:00:00 2001 From: trynafind-sumanyu Date: Tue, 5 May 2026 18:24:19 +0530 Subject: [PATCH 6/8] Fixed Formatting issues --- .../thealgorithms/recursion/Permutations.java | 8 ++--- .../thealgorithms/recursion/Permutations.java | 33 +++++++++---------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Permutations.java b/src/main/java/com/thealgorithms/recursion/Permutations.java index 6dda12db6957..dddbdfae5a8b 100644 --- a/src/main/java/com/thealgorithms/recursion/Permutations.java +++ b/src/main/java/com/thealgorithms/recursion/Permutations.java @@ -1,15 +1,11 @@ package com.thealgorithms.recursion; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** * This class provides methods to generate all permutations * of a given array of any type using recursion. - * + *

* Reference: * https://en.wikipedia.org/wiki/Permutation */ diff --git a/src/test/java/com/thealgorithms/recursion/Permutations.java b/src/test/java/com/thealgorithms/recursion/Permutations.java index 63930e3ec3a8..aa592afb3580 100644 --- a/src/test/java/com/thealgorithms/recursion/Permutations.java +++ b/src/test/java/com/thealgorithms/recursion/Permutations.java @@ -1,11 +1,10 @@ package com.thealgorithms.recursion; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; import java.util.List; -import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; class PermutationsTest { @@ -20,7 +19,7 @@ void testNullArrayThrowsNullPointerException() { @Test void testArrayWithNullElementThrowsIllegalArgumentException() { - Integer[] items = { 1, null, 3 }; + Integer[] items = {1, null, 3}; assertThrows(IllegalArgumentException.class, () -> Permutations.permutations(items)); } @@ -38,7 +37,7 @@ void testEmptyArrayReturnsOneEmptyPermutation() { @Test void testSingleElementReturnsOnePermutation() { - Integer[] items = { 42 }; + Integer[] items = {42}; List> result = Permutations.permutations(items); assertEquals(1, result.size()); assertEquals(List.of(42), result.get(0)); @@ -50,7 +49,7 @@ void testSingleElementReturnsOnePermutation() { @Test void testTwoIntegersReturnsTwoPermutations() { - Integer[] items = { 1, 2 }; + Integer[] items = {1, 2}; List> result = Permutations.permutations(items); assertEquals(2, result.size()); assertTrue(result.contains(List.of(1, 2))); @@ -59,14 +58,14 @@ void testTwoIntegersReturnsTwoPermutations() { @Test void testThreeIntegersReturnsSixPermutations() { - Integer[] items = { 1, 2, 3 }; + Integer[] items = {1, 2, 3}; List> result = Permutations.permutations(items); assertEquals(6, result.size()); } @Test void testIntegerPermutationsContainAllExpectedOrders() { - Integer[] items = { 1, 2, 3 }; + Integer[] items = {1, 2, 3}; List> result = Permutations.permutations(items); assertTrue(result.contains(List.of(1, 2, 3))); assertTrue(result.contains(List.of(1, 3, 2))); @@ -82,7 +81,7 @@ void testIntegerPermutationsContainAllExpectedOrders() { @Test void testTwoDuplicateIntegersReturnsOnePermutation() { - Integer[] items = { 1, 1 }; + Integer[] items = {1, 1}; List> result = Permutations.permutations(items); assertEquals(1, result.size()); assertEquals(List.of(1, 1), result.get(0)); @@ -90,7 +89,7 @@ void testTwoDuplicateIntegersReturnsOnePermutation() { @Test void testArrayWithDuplicatesReturnsCorrectCount() { - Integer[] items = { 1, 1, 2 }; + Integer[] items = {1, 1, 2}; List> result = Permutations.permutations(items); // 3!/2! = 3 unique permutations assertEquals(3, result.size()); @@ -101,7 +100,7 @@ void testArrayWithDuplicatesReturnsCorrectCount() { @Test void testAllDuplicatesReturnsOnePermutation() { - Integer[] items = { 5, 5, 5 }; + Integer[] items = {5, 5, 5}; List> result = Permutations.permutations(items); assertEquals(1, result.size()); assertEquals(List.of(5, 5, 5), result.get(0)); @@ -113,7 +112,7 @@ void testAllDuplicatesReturnsOnePermutation() { @Test void testTwoStringsReturnsTwoPermutations() { - String[] items = { "a", "b" }; + String[] items = {"a", "b"}; List> result = Permutations.permutations(items); assertEquals(2, result.size()); assertTrue(result.contains(List.of("a", "b"))); @@ -122,14 +121,14 @@ void testTwoStringsReturnsTwoPermutations() { @Test void testThreeStringsReturnsSixPermutations() { - String[] items = { "x", "y", "z" }; + String[] items = {"x", "y", "z"}; List> result = Permutations.permutations(items); assertEquals(6, result.size()); } @Test void testDuplicateStringsReturnsCorrectCount() { - String[] items = { "a", "a", "b" }; + String[] items = {"a", "a", "b"}; List> result = Permutations.permutations(items); assertEquals(3, result.size()); assertTrue(result.contains(List.of("a", "a", "b"))); @@ -143,14 +142,14 @@ void testDuplicateStringsReturnsCorrectCount() { @Test void testCharacterPermutations() { - Character[] items = { 'a', 'b', 'c' }; + Character[] items = {'a', 'b', 'c'}; List> result = Permutations.permutations(items); assertEquals(6, result.size()); } @Test void testDuplicateCharactersReturnsCorrectCount() { - Character[] items = { 'a', 'a', 'b' }; + Character[] items = {'a', 'a', 'b'}; List> result = Permutations.permutations(items); assertEquals(3, result.size()); } From bc0ebaf816ab389a9db71e527e7cdd608c0c814c Mon Sep 17 00:00:00 2001 From: trynafind-sumanyu Date: Tue, 5 May 2026 18:28:55 +0530 Subject: [PATCH 7/8] Test imports issue fix --- src/test/java/com/thealgorithms/recursion/Permutations.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/recursion/Permutations.java b/src/test/java/com/thealgorithms/recursion/Permutations.java index aa592afb3580..3df600362bbd 100644 --- a/src/test/java/com/thealgorithms/recursion/Permutations.java +++ b/src/test/java/com/thealgorithms/recursion/Permutations.java @@ -1,10 +1,12 @@ package com.thealgorithms.recursion; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; class PermutationsTest { From a8a8e13432ec3fb1c8b05b806a788939819e4702 Mon Sep 17 00:00:00 2001 From: trynafind-sumanyu Date: Tue, 5 May 2026 18:37:36 +0530 Subject: [PATCH 8/8] Fixing the formats --- src/main/java/com/thealgorithms/recursion/Permutations.java | 6 +++++- src/test/java/com/thealgorithms/recursion/Permutations.java | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/recursion/Permutations.java b/src/main/java/com/thealgorithms/recursion/Permutations.java index dddbdfae5a8b..5550a0dba5fd 100644 --- a/src/main/java/com/thealgorithms/recursion/Permutations.java +++ b/src/main/java/com/thealgorithms/recursion/Permutations.java @@ -1,6 +1,10 @@ package com.thealgorithms.recursion; -import java.util.*; +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 diff --git a/src/test/java/com/thealgorithms/recursion/Permutations.java b/src/test/java/com/thealgorithms/recursion/Permutations.java index 3df600362bbd..e548ee505daf 100644 --- a/src/test/java/com/thealgorithms/recursion/Permutations.java +++ b/src/test/java/com/thealgorithms/recursion/Permutations.java @@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; - import org.junit.jupiter.api.Test; class PermutationsTest {