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);
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";

  1. gao can on 2015-11-7

    Consider using a StringBuilder?

  2. sunandan on 2016-1-21

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

  3. gao can on 2016-1-21


  4. sunandan on 2016-1-21

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

  5. Joshua Liew on 2016-11-19

    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

Leave a comment