Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
72 lines (59 loc) · 2.4 KB

File metadata and controls

72 lines (59 loc) · 2.4 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

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("");
        }
    }
}
​
Morty Proxy This is a proxified and sanitized view of the page, visit original site.