Skip to content

Commit

Permalink
Merge pull request #454 from namita0210/Day-19-Q3
Browse files Browse the repository at this point in the history
Day 19 Q3
  • Loading branch information
bh-g authored Jan 23, 2024
2 parents a2136e2 + a91eb7d commit f1ba4a2
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class namita0210_java {

public static List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
if (digits == null || digits.length() == 0) {
return result;
}

Map<Character, String> digitToLetters = new HashMap<>();
digitToLetters.put('2', "abc");
digitToLetters.put('3', "def");
digitToLetters.put('4', "ghi");
digitToLetters.put('5', "jkl");
digitToLetters.put('6', "mno");
digitToLetters.put('7', "pqrs");
digitToLetters.put('8', "tuv");
digitToLetters.put('9', "wxyz");

generateCombinations(result, digits, digitToLetters, "", 0);

return result;
}

private static void generateCombinations(List<String> result, String digits, Map<Character, String> digitToLetters, String current, int index) {
if (index == digits.length()) {
result.add(current);
return;
}

char digit = digits.charAt(index);
String letters = digitToLetters.get(digit);

for (char letter : letters.toCharArray()) {
generateCombinations(result, digits, digitToLetters, current + letter, index + 1);
}
}


}

0 comments on commit f1ba4a2

Please sign in to comment.