From 61e2a9826e1833a3fb58a4f389eb52502522c59c Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 13:20:02 +0200 Subject: [PATCH 01/25] Refactor --- .../LetterCombinationsOfPhoneNumber.java | 90 ++++++++++++------- .../LetterCombinationsOfPhoneNumberTest.java | 63 ++++++------- 2 files changed, 86 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 2e3ee25fb6ea..6f99c5f3feeb 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -3,51 +3,77 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; public final class LetterCombinationsOfPhoneNumber { + + // Mapping of numbers to corresponding letters on a phone keypad + private static final Map numberToCharMap = Map.of( + 0, "", + 1, "", + 2, "abc", + 3, "def", + 4, "ghi", + 5, "jkl", + 6, "mno", + 7, "pqrs", + 8, "tuv", + 9, "wxyz" + ); + private LetterCombinationsOfPhoneNumber() { + // Prevent instantiation } - static Character[][] numberToCharMap; + /** + * Generates a list of all possible letter combinations that the provided + * array of numbers could represent on a phone keypad. + * + * @param numbers an array of integers representing the phone numbers + * @return a list of possible letter combinations + */ + public static List getCombinations(int[] numbers) { + if (numbers == null || numbers.length == 0) { + return List.of(""); + } + return generateCombinations(numbers, 0, new StringBuilder()); + } - protected static List printWords(int[] numbers, int len, int numIndex, String s) { - if (len == numIndex) { - return new ArrayList<>(Collections.singleton(s)); + /** + * Recursive method to generate combinations of letters from the phone keypad. + * + * @param numbers the input array of phone numbers + * @param index the current index in the numbers array being processed + * @param current a StringBuilder holding the current combination of letters + * @return a list of letter combinations formed from the given numbers + */ + private static List generateCombinations(int[] numbers, int index, StringBuilder current) { + // Base case: if we've processed all numbers, return the current combination + if (index == numbers.length) { + return new ArrayList<>(Collections.singletonList(current.toString())); } - List stringList = new ArrayList<>(); + List combinations = new ArrayList<>(); + String letters = numberToCharMap.get(numbers[index]); // Get corresponding letters for the current number - for (int i = 0; i < numberToCharMap[numbers[numIndex]].length; i++) { - String sCopy = String.copyValueOf(s.toCharArray()); - sCopy = sCopy.concat(numberToCharMap[numbers[numIndex]][i].toString()); - stringList.addAll(printWords(numbers, len, numIndex + 1, sCopy)); + // Iterate over each letter and recurse to generate further combinations + for (char letter : letters.toCharArray()) { + current.append(letter); // Append the current letter + combinations.addAll(generateCombinations(numbers, index + 1, current)); // Recursive call + current.deleteCharAt(current.length() - 1); // Backtrack by removing the last appended letter } - return stringList; - } - - private static void printWords(int[] numbers) { - generateNumberToCharMap(); - List stringList = printWords(numbers, numbers.length, 0, ""); - stringList.stream().forEach(System.out::println); - } - protected static void generateNumberToCharMap() { - numberToCharMap = new Character[10][5]; - numberToCharMap[0] = new Character[] {'\0'}; - numberToCharMap[1] = new Character[] {'\0'}; - numberToCharMap[2] = new Character[] {'a', 'b', 'c'}; - numberToCharMap[3] = new Character[] {'d', 'e', 'f'}; - numberToCharMap[4] = new Character[] {'g', 'h', 'i'}; - numberToCharMap[5] = new Character[] {'j', 'k', 'l'}; - numberToCharMap[6] = new Character[] {'m', 'n', 'o'}; - numberToCharMap[7] = new Character[] {'p', 'q', 'r', 's'}; - numberToCharMap[8] = new Character[] {'t', 'u', 'v'}; - numberToCharMap[9] = new Character[] {'w', 'x', 'y', 'z'}; + return combinations; } - // Driver code + /** + * Main method for testing the letter combination generation. + * + * @param args command line arguments + */ public static void main(String[] args) { - int[] number = {2, 3, 4}; - printWords(number); + int[] numbers = {2, 3, 4}; + List combinations = getCombinations(numbers); + combinations.forEach(System.out::println); // Print each combination } } diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index 4ffbddcb44a8..adc2a725f125 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -1,45 +1,38 @@ package com.thealgorithms.strings; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.util.Arrays; import java.util.List; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class LetterCombinationsOfPhoneNumberTest { - @Test - public void letterCombinationsOfPhoneNumber() { - LetterCombinationsOfPhoneNumber.generateNumberToCharMap(); - - // ** Test 1 ** - // Input: digits = "" - // Output: [] - int[] numbers1 = {}; - List output1 = Arrays.asList(""); - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers1, numbers1.length, 0, "").equals(output1)); - - // ** Test 2 ** - // Input: digits = "2" - // Output: ["a","b","c"] - int[] numbers2 = {2}; - List output2 = Arrays.asList("a", "b", "c"); - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers2, numbers2.length, 0, "").equals(output2)); - - // ** Test 3 ** - // Input: digits = "23" - // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] - int[] numbers3 = {2, 3}; - List output3 = Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"); - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers3, numbers3.length, 0, "").equals(output3)); - - // ** Test 4 ** - // Input: digits = "234" - // Output: ["adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", - // "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", - // "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"] - int[] numbers4 = {2, 3, 4}; - List output4 = Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"); - assertTrue(LetterCombinationsOfPhoneNumber.printWords(numbers4, numbers4.length, 0, "").equals(output4)); + @ParameterizedTest + @MethodSource("provideTestCases") + public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expectedOutput) { + assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); + } + + private static Stream provideTestCases() { + return Stream.of( + // Test case 1: + Arguments.of(new int[]{}, List.of("")), + + // Test case 2: + Arguments.of(new int[]{2}, Arrays.asList("a", "b", "c")), + + // Test case 3: + Arguments.of(new int[]{2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), + + // Test case 4: + Arguments.of(new int[]{2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", + "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", + "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")) + ); } } From 93252bfe4d8eb310982d037a9d25a5e9f3877e24 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 13:32:49 +0200 Subject: [PATCH 02/25] fix clang --- .../LetterCombinationsOfPhoneNumber.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 6f99c5f3feeb..36ad794f02a6 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -8,18 +8,17 @@ public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final Map numberToCharMap = Map.of( - 0, "", - 1, "", - 2, "abc", - 3, "def", - 4, "ghi", - 5, "jkl", - 6, "mno", - 7, "pqrs", - 8, "tuv", - 9, "wxyz" - ); + private static final Map numberToCharMap = Map.of( // + 0, "", // + 1, "", // + 2, "abc", // + 3, "def", // + 4, "ghi", // + 5, "jkl", // + 6, "mno", // + 7, "pqrs", // + 8, "tuv", // + 9, "wxyz"); private LetterCombinationsOfPhoneNumber() { // Prevent instantiation From 96bdb98d3210f091e7431655a6292bbb127b0972 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 13:32:49 +0200 Subject: [PATCH 03/25] fix clang --- .../LetterCombinationsOfPhoneNumber.java | 23 +++++++++---------- .../LetterCombinationsOfPhoneNumberTest.java | 9 ++++---- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 6f99c5f3feeb..36ad794f02a6 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -8,18 +8,17 @@ public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final Map numberToCharMap = Map.of( - 0, "", - 1, "", - 2, "abc", - 3, "def", - 4, "ghi", - 5, "jkl", - 6, "mno", - 7, "pqrs", - 8, "tuv", - 9, "wxyz" - ); + private static final Map numberToCharMap = Map.of( // + 0, "", // + 1, "", // + 2, "abc", // + 3, "def", // + 4, "ghi", // + 5, "jkl", // + 6, "mno", // + 7, "pqrs", // + 8, "tuv", // + 9, "wxyz"); private LetterCombinationsOfPhoneNumber() { // Prevent instantiation diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index adc2a725f125..d048fb4169b8 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -1,14 +1,13 @@ package com.thealgorithms.strings; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; public class LetterCombinationsOfPhoneNumberTest { From bfb4d32530b6ff8b995e57f50b4836a79df8dd15 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 13:38:24 +0200 Subject: [PATCH 04/25] fix clang tests --- .../LetterCombinationsOfPhoneNumberTest.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index d048fb4169b8..9b0a96d8e2d3 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -19,19 +19,19 @@ public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expe private static Stream provideTestCases() { return Stream.of( - // Test case 1: - Arguments.of(new int[]{}, List.of("")), + // Test case 1: + Arguments.of(new int[] {}, List.of("")), - // Test case 2: - Arguments.of(new int[]{2}, Arrays.asList("a", "b", "c")), + // Test case 2: + Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), - // Test case 3: - Arguments.of(new int[]{2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), + // Test case 3: + Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), - // Test case 4: - Arguments.of(new int[]{2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", - "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", - "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")) - ); + // Test case 4: + Arguments.of(new int[] {2, 3, 4}, + Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", // + "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", // + "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"))); } } From b228744265b578f6933807debbe158cee757f735 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 14:08:35 +0200 Subject: [PATCH 05/25] fix pattern --- .../strings/LetterCombinationsOfPhoneNumber.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 36ad794f02a6..34e670607c45 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -8,7 +8,7 @@ public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final Map numberToCharMap = Map.of( // + private static final Map MAP_OF_CHARS = Map.of( // 0, "", // 1, "", // 2, "abc", // @@ -53,7 +53,7 @@ private static List generateCombinations(int[] numbers, int index, Strin } List combinations = new ArrayList<>(); - String letters = numberToCharMap.get(numbers[index]); // Get corresponding letters for the current number + String letters = MAP_OF_CHARS.get(numbers[index]); // Get corresponding letters for the current number // Iterate over each letter and recurse to generate further combinations for (char letter : letters.toCharArray()) { From b1a6124c54905676926ddbc15139549153417dec Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 14:11:01 +0200 Subject: [PATCH 06/25] add test case null --- .../strings/LetterCombinationsOfPhoneNumberTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index 9b0a96d8e2d3..f72db440e153 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -19,7 +19,10 @@ public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expe private static Stream provideTestCases() { return Stream.of( - // Test case 1: + // Test case null: + Arguments.of(null, List.of("")), + + // Tesc case 1: Arguments.of(new int[] {}, List.of("")), // Test case 2: From 667403b87f41d1fe5acf23d22ba3681a62b58b82 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:14:27 +0200 Subject: [PATCH 07/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../strings/LetterCombinationsOfPhoneNumber.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 34e670607c45..1383b4bc0741 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -8,17 +8,7 @@ public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final Map MAP_OF_CHARS = Map.of( // - 0, "", // - 1, "", // - 2, "abc", // - 3, "def", // - 4, "ghi", // - 5, "jkl", // - 6, "mno", // - 7, "pqrs", // - 8, "tuv", // - 9, "wxyz"); + private static final Map MAP_OF_CHARS = Map.of(0, "", 1, "", 2, "abc", 3, "def", 4, "ghi", 5, "jkl", 6, "mno", 7, "pqrs", 8, "tuv", 9, "wxyz"); private LetterCombinationsOfPhoneNumber() { // Prevent instantiation From 48f23e2752d8b8ab87f876c7e7f5652d3fbf7820 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:14:46 +0200 Subject: [PATCH 08/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 1383b4bc0741..f85de32dc2ef 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -11,7 +11,6 @@ public final class LetterCombinationsOfPhoneNumber { private static final Map MAP_OF_CHARS = Map.of(0, "", 1, "", 2, "abc", 3, "def", 4, "ghi", 5, "jkl", 6, "mno", 7, "pqrs", 8, "tuv", 9, "wxyz"); private LetterCombinationsOfPhoneNumber() { - // Prevent instantiation } /** From e1a428c0e0a89da9682363c51ce57fdc9d590424 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:14:57 +0200 Subject: [PATCH 09/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index f85de32dc2ef..7d0b6273933c 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -21,7 +21,7 @@ private LetterCombinationsOfPhoneNumber() { * @return a list of possible letter combinations */ public static List getCombinations(int[] numbers) { - if (numbers == null || numbers.length == 0) { + if (numbers == null) { return List.of(""); } return generateCombinations(numbers, 0, new StringBuilder()); From cc069a8bbda3c902843b9875e88cd48d8fe2c74a Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:15:03 +0200 Subject: [PATCH 10/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 7d0b6273933c..1e3a469e4ca9 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -42,7 +42,7 @@ private static List generateCombinations(int[] numbers, int index, Strin } List combinations = new ArrayList<>(); - String letters = MAP_OF_CHARS.get(numbers[index]); // Get corresponding letters for the current number + final String letters = MAP_OF_CHARS.get(numbers[index]); // Get corresponding letters for the current number // Iterate over each letter and recurse to generate further combinations for (char letter : letters.toCharArray()) { From fda621bc7281732439c48583720d2f561a514719 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:15:28 +0200 Subject: [PATCH 11/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 1e3a469e4ca9..bdf0ba2d6134 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -47,7 +47,7 @@ private static List generateCombinations(int[] numbers, int index, Strin // Iterate over each letter and recurse to generate further combinations for (char letter : letters.toCharArray()) { current.append(letter); // Append the current letter - combinations.addAll(generateCombinations(numbers, index + 1, current)); // Recursive call + combinations.addAll(generateCombinations(numbers, index + 1, current)); current.deleteCharAt(current.length() - 1); // Backtrack by removing the last appended letter } From d8ace832b87f1a82d8fe0043cf1918129b71ab26 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:15:34 +0200 Subject: [PATCH 12/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index bdf0ba2d6134..1cef39bb68e2 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -46,7 +46,7 @@ private static List generateCombinations(int[] numbers, int index, Strin // Iterate over each letter and recurse to generate further combinations for (char letter : letters.toCharArray()) { - current.append(letter); // Append the current letter + current.append(letter); combinations.addAll(generateCombinations(numbers, index + 1, current)); current.deleteCharAt(current.length() - 1); // Backtrack by removing the last appended letter } From b257d14cbb4cfee5b7e270fa351b54705c464a28 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:15:41 +0200 Subject: [PATCH 13/25] Update src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../LetterCombinationsOfPhoneNumberTest.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index f72db440e153..980cab6acdd0 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -17,24 +17,8 @@ public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expe assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); } - private static Stream provideTestCases() { - return Stream.of( - // Test case null: - Arguments.of(null, List.of("")), - - // Tesc case 1: - Arguments.of(new int[] {}, List.of("")), - - // Test case 2: - Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), - - // Test case 3: - Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), - - // Test case 4: - Arguments.of(new int[] {2, 3, 4}, - Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", // - "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", // - "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"))); + private static Stream provideTestCases() { + return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), + Arguments.of(new int[] {2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"))); } } From 575b5e10a2a1fdc1f786a36fcdd2050a6ec50587 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 17:17:46 +0200 Subject: [PATCH 14/25] rename MAP_OF_CHARS to KEYPAD --- .../strings/LetterCombinationsOfPhoneNumber.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 1cef39bb68e2..1428f175f2aa 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -8,7 +8,7 @@ public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final Map MAP_OF_CHARS = Map.of(0, "", 1, "", 2, "abc", 3, "def", 4, "ghi", 5, "jkl", 6, "mno", 7, "pqrs", 8, "tuv", 9, "wxyz"); + private static final Map KEYPAD = Map.of(0, "", 1, "", 2, "abc", 3, "def", 4, "ghi", 5, "jkl", 6, "mno", 7, "pqrs", 8, "tuv", 9, "wxyz"); private LetterCombinationsOfPhoneNumber() { } @@ -42,7 +42,7 @@ private static List generateCombinations(int[] numbers, int index, Strin } List combinations = new ArrayList<>(); - final String letters = MAP_OF_CHARS.get(numbers[index]); // Get corresponding letters for the current number + final String letters = KEYPAD.get(numbers[index]); // Get corresponding letters for the current number // Iterate over each letter and recurse to generate further combinations for (char letter : letters.toCharArray()) { From 909acdba4feceb2738dcdd0eb3513cd766719266 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 17:18:59 +0200 Subject: [PATCH 15/25] fix clang --- .../strings/LetterCombinationsOfPhoneNumberTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index 980cab6acdd0..d80e85803bfa 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -17,7 +17,7 @@ public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expe assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); } - private static Stream provideTestCases() { + private static Stream provideTestCases() { return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), Arguments.of(new int[] {2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"))); } From 1c100efd021a1c3e2f1e22176632293713ed7fa0 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 17:21:13 +0200 Subject: [PATCH 16/25] remove main --- .../strings/LetterCombinationsOfPhoneNumber.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 1428f175f2aa..11fff617c7b1 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -53,15 +53,4 @@ private static List generateCombinations(int[] numbers, int index, Strin return combinations; } - - /** - * Main method for testing the letter combination generation. - * - * @param args command line arguments - */ - public static void main(String[] args) { - int[] numbers = {2, 3, 4}; - List combinations = getCombinations(numbers); - combinations.forEach(System.out::println); // Print each combination - } } From 97e26d3f4850d38cd0b1c6a604fad0bf71a41042 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Wed, 12 Jun 2024 17:50:37 +0200 Subject: [PATCH 17/25] add tests --- .../strings/LetterCombinationsOfPhoneNumber.java | 2 +- .../strings/LetterCombinationsOfPhoneNumberTest.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 11fff617c7b1..ccc176e828f7 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -8,7 +8,7 @@ public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final Map KEYPAD = Map.of(0, "", 1, "", 2, "abc", 3, "def", 4, "ghi", 5, "jkl", 6, "mno", 7, "pqrs", 8, "tuv", 9, "wxyz"); + private static final Map KEYPAD = Map.of(0, " ", 1, "", 2, "abc", 3, "def", 4, "ghi", 5, "jkl", 6, "mno", 7, "pqrs", 8, "tuv", 9, "wxyz"); private LetterCombinationsOfPhoneNumber() { } diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index d80e85803bfa..a4b7783b4b3b 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -19,6 +19,8 @@ public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expe private static Stream provideTestCases() { return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), - Arguments.of(new int[] {2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi"))); + Arguments.of(new int[] {2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")), + Arguments.of(new int[] {3, 3}, Arrays.asList("dd", "de", "df", "ed", "ee", "ef", "fd", "fe", "ff")), Arguments.of(new int[] {8, 4}, Arrays.asList("tg", "th", "ti", "ug", "uh", "ui", "vg", "vh", "vi")), Arguments.of(new int[] {2, 0}, Arrays.asList("a ", "b ", "c ")), + Arguments.of(new int[] {9, 2}, Arrays.asList("wa", "wb", "wc", "xa", "xb", "xc", "ya", "yb", "yc", "za", "zb", "zc"))); } } From 910c814d2a7152abcd5a4a03f161463f45d65402 Mon Sep 17 00:00:00 2001 From: vil02 Date: Wed, 12 Jun 2024 20:10:40 +0200 Subject: [PATCH 18/25] feat: throw for wrong inputs --- .../strings/LetterCombinationsOfPhoneNumber.java | 7 ++++++- .../strings/LetterCombinationsOfPhoneNumberTest.java | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index ccc176e828f7..53c9c0564fee 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -41,8 +41,13 @@ private static List generateCombinations(int[] numbers, int index, Strin return new ArrayList<>(Collections.singletonList(current.toString())); } + final var number = numbers[index]; + if (number < 0 || number > 9) { + throw new IllegalArgumentException("Input numbers must in the range [0, 9]"); + } + + final String letters = KEYPAD.get(number); // Get corresponding letters for the current number List combinations = new ArrayList<>(); - final String letters = KEYPAD.get(numbers[index]); // Get corresponding letters for the current number // Iterate over each letter and recurse to generate further combinations for (char letter : letters.toCharArray()) { diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index a4b7783b4b3b..dca3fd83029a 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.strings; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Arrays; import java.util.List; @@ -17,10 +18,20 @@ public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expe assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); } + @ParameterizedTest + @MethodSource("wrongInputs") + void throwsForWrongInput(int[] numbers) { + assertThrows(IllegalArgumentException.class, () -> LetterCombinationsOfPhoneNumber.getCombinations(numbers)); + } + private static Stream provideTestCases() { return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), Arguments.of(new int[] {2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")), Arguments.of(new int[] {3, 3}, Arrays.asList("dd", "de", "df", "ed", "ee", "ef", "fd", "fe", "ff")), Arguments.of(new int[] {8, 4}, Arrays.asList("tg", "th", "ti", "ug", "uh", "ui", "vg", "vh", "vi")), Arguments.of(new int[] {2, 0}, Arrays.asList("a ", "b ", "c ")), Arguments.of(new int[] {9, 2}, Arrays.asList("wa", "wb", "wc", "xa", "xb", "xc", "ya", "yb", "yc", "za", "zb", "zc"))); } + + private static Stream wrongInputs() { + return Stream.of(Arguments.of(new int[] {-1}), Arguments.of(new int[] {10}), Arguments.of(new int[] {2, 2, -1, 0}), Arguments.of(new int[] {0, 0, 0, 10})); + } } From 06d49ced305423dba456fcb710a74506b2169b57 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Thu, 13 Jun 2024 10:21:21 +0200 Subject: [PATCH 19/25] change keypad to list --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 53c9c0564fee..b7ed86162296 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -3,12 +3,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final Map KEYPAD = Map.of(0, " ", 1, "", 2, "abc", 3, "def", 4, "ghi", 5, "jkl", 6, "mno", 7, "pqrs", 8, "tuv", 9, "wxyz"); + private static final List KEYPAD = List.of(" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"); private LetterCombinationsOfPhoneNumber() { } From 41c70077b8d0efbb606dcd8b5a2b5686cdf429be Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Thu, 13 Jun 2024 11:18:43 +0200 Subject: [PATCH 20/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index b7ed86162296..6efc595d4610 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -7,7 +7,7 @@ public final class LetterCombinationsOfPhoneNumber { // Mapping of numbers to corresponding letters on a phone keypad - private static final List KEYPAD = List.of(" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"); + private static final String[] KEYPAD = new String[] {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; private LetterCombinationsOfPhoneNumber() { } From 3ef428b64ccc6d076af5b0e7c5769845604d5bf0 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Thu, 13 Jun 2024 11:18:50 +0200 Subject: [PATCH 21/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 6efc595d4610..65222888baa8 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -49,7 +49,7 @@ private static List generateCombinations(int[] numbers, int index, Strin List combinations = new ArrayList<>(); // Iterate over each letter and recurse to generate further combinations - for (char letter : letters.toCharArray()) { + for (char letter : KEYPAD[number].toCharArray()) { current.append(letter); combinations.addAll(generateCombinations(numbers, index + 1, current)); current.deleteCharAt(current.length() - 1); // Backtrack by removing the last appended letter From d3f8c036d5cbed9869db01c7aaec375c04d5c6f5 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello <4256795+samuelfac@users.noreply.github.com> Date: Thu, 13 Jun 2024 11:18:59 +0200 Subject: [PATCH 22/25] Update src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index 65222888baa8..ef9c3da2583f 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -45,7 +45,6 @@ private static List generateCombinations(int[] numbers, int index, Strin throw new IllegalArgumentException("Input numbers must in the range [0, 9]"); } - final String letters = KEYPAD.get(number); // Get corresponding letters for the current number List combinations = new ArrayList<>(); // Iterate over each letter and recurse to generate further combinations From 24697f9c55a617cd45435e1eb210cdabe1249158 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Thu, 13 Jun 2024 13:29:51 +0200 Subject: [PATCH 23/25] fix with number 1 (empty value), and add tests --- .../strings/LetterCombinationsOfPhoneNumber.java | 12 +++++++++--- .../LetterCombinationsOfPhoneNumberTest.java | 14 +++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index ef9c3da2583f..b1761b467498 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -6,8 +6,10 @@ public final class LetterCombinationsOfPhoneNumber { + private static final char EMPTY = '\0'; + // Mapping of numbers to corresponding letters on a phone keypad - private static final String[] KEYPAD = new String[] {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + private static final String[] KEYPAD = new String[] {" ", EMPTY + "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; private LetterCombinationsOfPhoneNumber() { } @@ -49,9 +51,13 @@ private static List generateCombinations(int[] numbers, int index, Strin // Iterate over each letter and recurse to generate further combinations for (char letter : KEYPAD[number].toCharArray()) { - current.append(letter); + if (letter != EMPTY) { + current.append(letter); + } combinations.addAll(generateCombinations(numbers, index + 1, current)); - current.deleteCharAt(current.length() - 1); // Backtrack by removing the last appended letter + if (letter != EMPTY) { + current.deleteCharAt(current.length() - 1); // Backtrack by removing the last appended letter + } } return combinations; diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index dca3fd83029a..456b1e872830 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -1,9 +1,8 @@ package com.thealgorithms.strings; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertLinesMatch; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.util.Arrays; import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; @@ -15,7 +14,7 @@ public class LetterCombinationsOfPhoneNumberTest { @ParameterizedTest @MethodSource("provideTestCases") public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expectedOutput) { - assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); + assertLinesMatch(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); } @ParameterizedTest @@ -25,10 +24,11 @@ void throwsForWrongInput(int[] numbers) { } private static Stream provideTestCases() { - return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), - Arguments.of(new int[] {2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")), - Arguments.of(new int[] {3, 3}, Arrays.asList("dd", "de", "df", "ed", "ee", "ef", "fd", "fe", "ff")), Arguments.of(new int[] {8, 4}, Arrays.asList("tg", "th", "ti", "ug", "uh", "ui", "vg", "vh", "vi")), Arguments.of(new int[] {2, 0}, Arrays.asList("a ", "b ", "c ")), - Arguments.of(new int[] {9, 2}, Arrays.asList("wa", "wb", "wc", "xa", "xb", "xc", "ya", "yb", "yc", "za", "zb", "zc"))); + return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, List.of("a", "b", "c")), Arguments.of(new int[] {2, 3}, List.of("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")), + Arguments.of(new int[] {2, 3, 4}, List.of("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")), + Arguments.of(new int[] {3, 3}, List.of("dd", "de", "df", "ed", "ee", "ef", "fd", "fe", "ff")), Arguments.of(new int[] {8, 4}, List.of("tg", "th", "ti", "ug", "uh", "ui", "vg", "vh", "vi")), Arguments.of(new int[] {2, 0}, List.of("a ", "b ", "c ")), + Arguments.of(new int[] {9, 2}, List.of("wa", "wb", "wc", "xa", "xb", "xc", "ya", "yb", "yc", "za", "zb", "zc")), Arguments.of(new int[] {0}, List.of(" ")), Arguments.of(new int[] {1}, List.of("")), Arguments.of(new int[] {2}, List.of("a", "b", "c")), + Arguments.of(new int[] {1, 2, 0, 4}, List.of("a g", "a h", "a i", "b g", "b h", "b i", "c g", "c h", "c i"))); } private static Stream wrongInputs() { From 5b39d4b1bc0e8e731c458f022574040fe4d9dff8 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Thu, 13 Jun 2024 13:52:19 +0200 Subject: [PATCH 24/25] style: avoid concatenation while populating `KEYPAD` --- .../thealgorithms/strings/LetterCombinationsOfPhoneNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java index b1761b467498..38c6bc13fa2a 100644 --- a/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java +++ b/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java @@ -9,7 +9,7 @@ public final class LetterCombinationsOfPhoneNumber { private static final char EMPTY = '\0'; // Mapping of numbers to corresponding letters on a phone keypad - private static final String[] KEYPAD = new String[] {" ", EMPTY + "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + private static final String[] KEYPAD = new String[] {" ", String.valueOf(EMPTY), "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; private LetterCombinationsOfPhoneNumber() { } From 87aff590b45f407ff8538d73641a1090f9122596 Mon Sep 17 00:00:00 2001 From: Samuel Facchinello Date: Thu, 13 Jun 2024 19:32:31 +0200 Subject: [PATCH 25/25] change to assertEquals --- .../strings/LetterCombinationsOfPhoneNumberTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java index 456b1e872830..dcdb7d5b3392 100644 --- a/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java +++ b/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java @@ -1,6 +1,6 @@ package com.thealgorithms.strings; -import static org.junit.jupiter.api.Assertions.assertLinesMatch; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; @@ -14,7 +14,7 @@ public class LetterCombinationsOfPhoneNumberTest { @ParameterizedTest @MethodSource("provideTestCases") public void testLetterCombinationsOfPhoneNumber(int[] numbers, List expectedOutput) { - assertLinesMatch(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); + assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers)); } @ParameterizedTest