LeetCode

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"].

Java

public class Solution {
    String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
    public List<String> letterCombinations(String digits) {
        List<String> rst = new ArrayList<String>();
        if(digits == null || digits.length() == 0) {
            return rst;
        }
        char[] letters = new char[digits.length()];
        creatLetters(letters, digits, rst, 0);
        return rst;
    }
    private void creatLetters(char[] letters, String digits, List<String> rst, int index) {
        if(index == digits.length()) {
            rst.add(new String(letters));
            return;
        }
        String dict = map[digits.charAt(index) - '0'];
        for(int i = 0; i < dict.length(); i++) {
            letters[index] = dict.charAt(i);
            creatLetters(letters, digits, rst, index + 1);
        }
    }
}