[LeetCode] 17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

Thought process:
  1. Store number => character mappings in a map.
  2. Iterate through the phone number and get every possible combination. Recursively add and delete character from a string builder. The base case is when the StringBuilder.length() == digits.length().
Solution 1:
 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
class Solution {
    private String[] map = { " ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    
    public List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<>();
        if (digits.length() == 0) {
            return combinations;
        }
        
        letterCombinations(digits, 0, "", combinations);
        return combinations;
    }
    
    private void letterCombinations(String digits, int index, String combination, List<String> combinations) {
        if (index == digits.length()) {
            combinations.add(combination);
            return;
        }
        
        int digit = digits.charAt(index) - '0';
        for (char c : map[digit].toCharArray()) {
            letterCombinations(digits, index + 1, combination + c, combinations);
        }
    }
}

Solution 2 (StringBuilder):
 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
class Solution {
    private String[] letters = { " ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    
    public List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<>();
        if (digits.length() == 0) {
            return combinations;
        }
        StringBuilder sb = new StringBuilder();
        letterCombinations(digits, sb, combinations);
        return combinations;
    }
    
    private void letterCombinations(String digits, StringBuilder sb, List<String> combinations) {
        if (sb.length() == digits.length()) {
            combinations.add(sb.toString());
            return;
        }
        
        String s = letters[digits.charAt(sb.length()) - '0'];
        for (int i = 0; i < s.length(); i++) {
            sb.append(s.charAt(i));
            letterCombinations(digits, sb, combinations);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}

Time complexity:
Say each number can map to m characters on average, and the phone number has n digits. The overall time complexity is O(m^n).

Comments

Post a Comment

Popular posts from this blog

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula