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

Note: Although the above answer is in lexicographical order, your answer could be in any order you want.

Tips:

FIFO queue, faster: 以234为例,先加入abc,然后提出a,加d,e,f存,再提出b,c,加d,e,f存,再提出ad,存adg,adh,adi,以此类推。

DFS:就是普通的dfs。。。

Code:

FIFO queue:

public class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<String>();
        if (digits.length() == 0) {
            return ans;
        }
        String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        ans.add("");
        for (int i = 0; i < digits.length(); i++) {
            int cur = Character.getNumericValue(digits.charAt(i));
            while (ans.get(0).length() == i) {
                String prev = ans.remove(0);
                for (char c : mapping[cur].toCharArray()) {
                    ans.add(prev + c);
                }
            }
        }
        return ans;
    }
}

DFS:

stringBuilder版

public class Solution {
    String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<String>();
        if (digits == null || digits.length() == 0) {
            return result;
        }
        helper(digits, new StringBuilder(), result, 0);
        return result;
    }

    private void helper(String digits, StringBuilder prev, List<String> result, int depth) {
        if (depth >= digits.length()) {
            result.add(prev.toString());
            return;
        }
        String letters = mapping[Character.getNumericValue(digits.charAt(depth))];
        for (char c : letters.toCharArray()) {
            prev.append(c);
            helper(digits, prev, result, depth + 1);
            prev.deleteCharAt(prev.length() - 1);
        }
    }
}

String版

   public class Solution {
        private static final String[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };

        public List<String> letterCombinations(String digits) {
            List<String> ret = new LinkedList<String>();
            combination("", digits, 0, ret);
            return ret;
        }

        private void combination(String prefix, String digits, int offset, List<String> ret) {
            if (offset >= digits.length()) {
                ret.add(prefix);
                return;
            }
            String letters = KEYS[(digits.charAt(offset) - '0')];
            for (int i = 0; i < letters.length(); i++) {
                combination(prefix + letters.charAt(i), digits, offset + 1, ret);
            }
        }
    }

results matching ""

    No results matching ""