Given a string containing digits from2-9inclusive, 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.

这题也没有必要backtracking,毕竟没有什么不valid的case,但是写成backtracking也并非不可以

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if(digits == null || digits.length() == 0)
            return res;

        String[] strs = new String[] {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        helper(res, digits, strs, "", 0);

        return res;
    }

    public void helper(List<String> res, String digits, String[] strs, String curr, int start) {
        if(start == digits.length()) {
            res.add(curr);
            return;
        }

        String s = strs[digits.charAt(start) - '0'];
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            helper(res, digits, strs, curr + c, start + 1);
        }
    }
}

results matching ""

    No results matching ""