1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| import java.util.ArrayList; import java.util.HashMap; import java.util.List;
public class LetterCombinations {
static HashMap<Character, List<String>> hashMap = new HashMap<>(); static { hashMap.put('2', List.of("a", "b", "c")); hashMap.put('3', List.of("d", "e", "f")); hashMap.put('4', List.of("g", "h", "i")); hashMap.put('5', List.of("j", "k", "l")); hashMap.put('6', List.of("m", "n", "o")); hashMap.put('7', List.of("p", "q", "r", "s")); hashMap.put('8', List.of("t", "u", "v")); hashMap.put('9', List.of("w", "x", "y", "z")); }
public static void main(String[] args) { String digits = "2345"; List<String> result = letterCombinations(digits); for (String s : result) { System.out.println(s); } System.out.println(result.size()); }
public static List<String> letterCombinationsNoRecursion(String digits) { if (digits.isEmpty()) { return List.of(); } char[] charArray = digits.toCharArray(); List<String> result = hashMap.get(charArray[0]); int length = digits.length(); if (length == 1) { return result; } int i = 1; while (i < length){ List<String> temp = new ArrayList<>(); for (String s1 : result) { for (String s2 : hashMap.get(charArray[i])) { temp.add(s1+s2); } } result = temp; i++; } return result; }
public static List<String> letterCombinations(String digits) { if (digits.isEmpty()) { return List.of(); } char[] charArray = digits.toCharArray(); List<String> result = hashMap.get(charArray[0]); int length = digits.length(); if (length == 1) { return result; } return combine(1, digits,digits.length(), result); }
private static List<String> combine(int i, String digits, int length, List<String> result) { if (i == length) { return result; } List<String> re = new ArrayList<>(); List<String> strings = hashMap.get(digits.charAt(i)); for (String s : result) { for (String string : strings) { re.add(s + string); } } return combine(++i, digits, length, re); } }
|