File tree Expand file tree Collapse file tree
main/java/com/thealgorithms/recursion
test/java/com/thealgorithms/recursion Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .thealgorithms .recursion ;
22
33import java .util .ArrayList ;
4- import java .util .Arrays ;
54import java .util .List ;
65
76/**
@@ -40,11 +39,7 @@ public static List<List<Integer>> permutations(int[] nums) {
4039 return result ;
4140 }
4241
43- private static void generateIntegerPermutations (
44- int index ,
45- List <Integer > list ,
46- List <List <Integer >> result
47- ) {
42+ private static void generateIntegerPermutations (int index , List <Integer > list , List <List <Integer >> result ) {
4843 if (index == list .size ()) {
4944 result .add (new ArrayList <>(list ));
5045 return ;
@@ -80,21 +75,16 @@ public static List<String> permutations(String s) {
8075 return result ;
8176 }
8277
83- private static void generateStringPermutations (
84- String prefix ,
85- String remaining ,
86- List <String > result
87- ) {
78+ private static void generateStringPermutations (String prefix , String remaining , List <String > result ) {
8879 if (remaining .isEmpty ()) {
8980 result .add (prefix );
9081 return ;
9182 }
9283
9384 for (int i = 0 ; i < remaining .length (); i ++) {
9485 char ch = remaining .charAt (i );
95- String next =
96- remaining .substring (0 , i ) + remaining .substring (i + 1 );
86+ String next = remaining .substring (0 , i ) + remaining .substring (i + 1 );
9787 generateStringPermutations (prefix + ch , next , result );
9888 }
9989 }
100- }
90+ }
Original file line number Diff line number Diff line change 11package com .thealgorithms .recursion ;
22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
45
56import java .util .List ;
67import org .junit .jupiter .api .Test ;
@@ -14,9 +15,19 @@ void testIntegerPermutations() {
1415 assertEquals (2 , result .size ());
1516 }
1617
18+ @ Test
19+ void testIntegerPermutationsNull () {
20+ assertThrows (NullPointerException .class , () -> Permutations .permutations ((int []) null ));
21+ }
22+
1723 @ Test
1824 void testStringPermutations () {
1925 List <String > result = Permutations .permutations ("ab" );
2026 assertEquals (2 , result .size ());
2127 }
22- }
28+
29+ @ Test
30+ void testStringPermutationsNull () {
31+ assertThrows (NullPointerException .class , () -> Permutations .permutations ((String ) null ));
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments