LeetCode – Letter Combinations of a Phone Number (Java)

Given a digit string, return all possible letter combinations that the number could represent. (Check out your cellphone to see the mappings) Input:Digit string "23", Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].


This problem can be solves by a typical DFS algorithm. DFS problems are very similar and can be solved by using a simple recursion. Check out the index page to see other DFS problems.

Java Solution

public List<String> letterCombinations(String digits) {
    HashMap<Integer, String> map = new HashMap<Integer, String>();
    map.put(2, "abc");
    map.put(3, "def");
    map.put(4, "ghi");
    map.put(5, "jkl");
    map.put(6, "mno");
    map.put(7, "pqrs");
    map.put(8, "tuv");
    map.put(9, "wxyz");
    map.put(0, "");
    ArrayList<String> result = new ArrayList<String>();
    if(digits == null || digits.length() == 0)
        return result;
    ArrayList<Character> temp = new ArrayList<Character>();
    getString(digits, temp, result, map);
    return result;
public void getString(String digits, ArrayList<Character> temp, ArrayList<String> result,  HashMap<Integer, String> map){
    if(digits.length() == 0){
        char[] arr = new char[temp.size()];
        for(int i=0; i<temp.size(); i++){
            arr[i] = temp.get(i);
    Integer curr = Integer.valueOf(digits.substring(0,1));
    String letters = map.get(curr);
    for(int i=0; i<letters.length(); i++){
        getString(digits.substring(1), temp, result, map);

I often found that I write a solution differently each time I solve a problem. Here is another way of writing the solution.

public List<String> letterCombinations(String digits) {
    HashMap<Character, char[]> map = new HashMap<Character, char[]>();
    map.put('2', new char[]{'a','b','c'});
    map.put('3', new char[]{'d','e','f'});
    map.put('4', new char[]{'g','h','i'});
    map.put('5', new char[]{'j','k','l'});
    map.put('6', new char[]{'m','n','o'});
    map.put('7', new char[]{'p','q','r','s'});
    map.put('8', new char[]{'t','u','v'});
    map.put('9', new char[]{'w','x','y','z'});
    List<String> result = new ArrayList<String>();
        return result;
    helper(result, new StringBuilder(), digits, 0, map);
    return result;
public void helper(List<String> result, StringBuilder sb, String digits, int index, HashMap<Character, char[]> map){
    char c = digits.charAt(index);
    char[] arr = map.get(c);
    for(int i=0; i<arr.length; i++){
        helper(result, sb, digits, index+1, map);

If we use a char array to store each candidate, the code can be much more precise.

public List<String> letterCombinations(String digits) {
    HashMap<Character, char[]> dict = new HashMap<Character, char[]>();
    dict.put('2',new char[]{'a','b','c'});
    dict.put('3',new char[]{'d','e','f'});
    dict.put('4',new char[]{'g','h','i'});
    dict.put('5',new char[]{'j','k','l'});
    dict.put('6',new char[]{'m','n','o'});
    dict.put('7',new char[]{'p','q','r','s'});
    dict.put('8',new char[]{'t','u','v'});
    dict.put('9',new char[]{'w','x','y','z'});
    List<String> result = new ArrayList<String>();
        return result;
    char[] arr = new char[digits.length()];
    helper(digits, 0, dict, result, arr);
    return result;
private void helper(String digits, int index, HashMap<Character, char[]> dict, List<String> result, char[] arr){
        result.add(new String(arr));
    char number = digits.charAt(index);
    char[] candidates = dict.get(number);
    for(int i=0; i<candidates.length; i++){
        helper(digits, index+1, dict, result, arr);
Category >> Algorithms >> Interview >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Martin

    I think it’s not O(n^n) but O(m^n) where m is max number of letters possible for one digit and n is number of digits, so in case of keypad it’s O(4^n)

  • Joshua Liew

    I think it’s O(n^n) n = number of digits
    and space is O(m) m= number of unique combinations for every recursive calls

  • sunandan

    What is the time complexity of this ? Is it n^2 ?

  • gao can


  • sunandan

    your solution is much better . @author : consider changing the code to use this snippet. Much clearer.

  • gao can

    Consider using a StringBuilder?