|
| 1 | +package competitiveProgramming.leetcode.topinterviewquestions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/* |
| 7 | +17. Letter Combinations of a Phone Number |
| 8 | +
|
| 9 | +Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. |
| 10 | +
|
| 11 | +A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. |
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: digits = "23" |
| 16 | +Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] |
| 17 | +Example 2: |
| 18 | +
|
| 19 | +Input: digits = "" |
| 20 | +Output: [] |
| 21 | +Example 3: |
| 22 | +
|
| 23 | +Input: digits = "2" |
| 24 | +Output: ["a","b","c"] |
| 25 | +
|
| 26 | +
|
| 27 | +Constraints: |
| 28 | +
|
| 29 | +0 <= digits.length <= 4 |
| 30 | +digits[i] is a digit in the range ['2', '9']. |
| 31 | + */ |
| 32 | + |
| 33 | +public class LetterCombinationOfPhoneNumber { |
| 34 | + public static void main(String[] args) { |
| 35 | + System.out.println(letterCombinations("23")); |
| 36 | + System.out.println(letterCombinations("")); |
| 37 | + System.out.println(letterCombinations("2")); |
| 38 | + } |
| 39 | + |
| 40 | + public static List<String> letterCombinations(String digits) { |
| 41 | + List<String> combinations = new ArrayList<>(); |
| 42 | + |
| 43 | + if (digits == null) {return combinations;} |
| 44 | + char[] digits_ = digits.toCharArray(); |
| 45 | + |
| 46 | + for (char c : digits_) { |
| 47 | + permute(c, combinations); |
| 48 | + } |
| 49 | + |
| 50 | + return combinations; |
| 51 | + } |
| 52 | + |
| 53 | + private static void permute(char c, List<String> combinations) { |
| 54 | + char[] mappings = getMappings(c); |
| 55 | + |
| 56 | + if (combinations.size() == 0) { |
| 57 | + for (char mapping : mappings) { |
| 58 | + combinations.add(String.valueOf(mapping)); |
| 59 | + } |
| 60 | + } |
| 61 | + else { |
| 62 | + List<String> copy = new ArrayList<>(combinations); |
| 63 | + for (String current : copy) { |
| 64 | + combinations.remove(current); |
| 65 | + |
| 66 | + for (char mapping : mappings) { |
| 67 | + combinations.add(current + mapping); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public static char[] getMappings(char c) { |
| 75 | + char[] mappings = new char[]{}; |
| 76 | + switch (c) { |
| 77 | + case '2': { |
| 78 | + mappings = new char[]{'a', 'b', 'c'}; |
| 79 | + break; |
| 80 | + } |
| 81 | + case '3': { |
| 82 | + mappings = new char[]{'d', 'e', 'f'}; |
| 83 | + break; |
| 84 | + } |
| 85 | + case '4': { |
| 86 | + mappings = new char[]{'g', 'h', 'i'}; |
| 87 | + break; |
| 88 | + } |
| 89 | + case '5': { |
| 90 | + mappings = new char[]{'j', 'k', 'l'}; |
| 91 | + break; |
| 92 | + } |
| 93 | + case '6': { |
| 94 | + mappings = new char[]{'m', 'n', 'o'}; |
| 95 | + break; |
| 96 | + } |
| 97 | + case '7': { |
| 98 | + mappings = new char[]{'p', 'q', 'r', 's'}; |
| 99 | + break; |
| 100 | + } |
| 101 | + case '8': { |
| 102 | + mappings = new char[]{'t', 'u', 'v'}; |
| 103 | + break; |
| 104 | + } |
| 105 | + case '9': { |
| 106 | + mappings = new char[]{'w', 'x', 'y', 'z'}; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return mappings; |
| 112 | + } |
| 113 | +} |
0 commit comments