|
| 1 | +package com.algo.lc.recursion.backtrack; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +/** |
| 8 | + * Given a string containing digits from 2-9 inclusive, return all possible letter |
| 9 | + * combinations that the number could represent. Return the answer in any order. |
| 10 | + * |
| 11 | + * A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. |
| 12 | + */ |
| 13 | +public class LetterCombinationsInPhoneNumber { |
| 14 | + static Map<Character, String> numToChars = Map.of( |
| 15 | + '2', "abc", |
| 16 | + '3', "def", |
| 17 | + '4', "ghi", |
| 18 | + '5', "jkl", |
| 19 | + '6', "mno", |
| 20 | + '7', "pqrs", |
| 21 | + '8', "tuv", |
| 22 | + '9', "wxyz" |
| 23 | + ); |
| 24 | + /** |
| 25 | + * Complexity Analysis |
| 26 | +
|
| 27 | + Time complexity:O(4^N*N), where N is the length of digits. Note that 4 in this expression |
| 28 | + is referring to the maximum value length in the hash map, and not to the length of the input. |
| 29 | +
|
| 30 | + The worst-case is where the input consists of only 7s and 9s. In that case, we have to |
| 31 | + explore 4 additional paths for every extra digit. Then, for each combination, it costs |
| 32 | + up to N to build the combination. This problem can be generalized to a scenario where |
| 33 | + numbers correspond with up to M digits, in which case the time complexity would be |
| 34 | + O(M^N⋅N). For the problem constraints, we're given, M=4, because of digits 7 and 9 |
| 35 | + having 4 letters each. |
| 36 | +
|
| 37 | + Space complexity: O(N), where N is the length of digits. |
| 38 | +
|
| 39 | + Not counting space used for the output, the extra space we use relative to input size is the |
| 40 | + space occupied by the recursion call stack. It will only go as deep as the number of digits in |
| 41 | + the input since whenever we reach that depth, we backtrack. |
| 42 | +
|
| 43 | + As the hash map does not grow as the inputs grows, it occupies O(1) space. |
| 44 | + */ |
| 45 | + |
| 46 | + public List<String> letterCombinations(String digits) { |
| 47 | + List<String> result = new ArrayList<>(); |
| 48 | + if (digits == null || digits.length() == 0) { |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + StringBuilder combination = new StringBuilder(); |
| 53 | + helper(digits, result, combination, 0); |
| 54 | + |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + private void helper(String digits, |
| 59 | + List<String> result, |
| 60 | + StringBuilder combination, |
| 61 | + int index) { |
| 62 | + if (index == digits.length()) { |
| 63 | + result.add(combination.toString()); |
| 64 | + return; |
| 65 | + } |
| 66 | + char digit = digits.charAt(index); |
| 67 | + String value = numToChars.get(digit); |
| 68 | + for (char c : value.toCharArray()) { |
| 69 | + combination.append(c); |
| 70 | + helper(digits, result, combination, index + 1); |
| 71 | + combination.deleteCharAt(combination.length() - 1); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments