Skip to content

Latest commit

 

History

History
72 lines (59 loc) · 2.4 KB

File metadata and controls

72 lines (59 loc) · 2.4 KB

Difficulty: Medium

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:

Input: "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.

Solution

Language: Java

class Solution {
    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0) {
            return Collections.emptyList();
        }
        if (digits.length() == 1) {
            return singleDigitToStringList(digits.charAt(0));
        }
        List<String> leftArr = this.letterCombinations(digits.substring(0, digits.length() / 2));
        List<String> rightArr = this.letterCombinations(digits.substring(digits.length() / 2));
        List<String> result = new ArrayList<>();
        for (String left : leftArr) {
            for (String right : rightArr) {
                result.add(left + right);
            }
        }
        return result;
    }
​
    private List<String> singleDigitToStringList(char digit) {
        switch (digit) {
            case '2':
                return Arrays.asList("a", "b", "c");
            case '3':
                return Arrays.asList("d", "e", "f");
            case '4':
                return Arrays.asList("g", "h", "i");
            case '5':
                return Arrays.asList("j", "k", "l");
            case '6':
                return Arrays.asList("m", "n", "o");
            case '7':
                return Arrays.asList("p", "q", "r", "s");
            case '8':
                return Arrays.asList("t", "u", "v");
            case '9':
                return Arrays.asList("w", "x", "y", "z");
            default:
                return Arrays.asList("");
        }
    }
}
​