LeetCode17. Letter Combinations of a Phone Number Phone number combination

17. The telephone number combination

17. Letter Combinations of a Phone Number

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

Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Depth-first traversal (DFS)

First, we define a mapping array map on the phone keypad, such as 2 corresponds abc. This is represented using the subscript numbers, so the value is set to 0 and the subscript 1 is an empty string.

We can think of a tree to depth-first traversal. Wherein the number represents the length of the tree layers. And for a certain level, the value of all nodes are mapped to the layer should numeric characters. 2 The digital value corresponding to the number of layers will be for the node a, b, c. Then the depth traversed, i.e., to the root end of the string to the end of the traverse.

class Solution {
    private String[] map = {
    "",
    "",
    "abc",
    "def",
    "ghi",
    "jkl",
    "mno",
    "pqrs",
    "tuv",
    "wxyz"
    };
    private List<String> result;
    public List<String> letterCombinations(String digits) {
        result = new ArrayList<>();
        if(digits == null || digits.length() == 0){
            return result;
        }
        
        combineDigits(digits,0,new StringBuilder());
        return result;
    }
    
    public void combineDigits(String digits,int index,StringBuilder sb){
        if(index == digits.length()){
            result.add(sb.toString());
            return;
        }
        
        char c = digits.charAt(index);
        String letters = map[c - '0'];
        for(int i = 0; i < letters.length(); i++){
            combineDigits(digits,index + 1, sb.append(letters.charAt(i)));
            sb.deleteCharAt(sb.length() - 1);
        }
    }       
}
Direct splicing

Direct stitching is very simple scanning numbers, each number corresponding to the character string obtained before and spliced, and then update the strings.

class Solution {
    Map<Character,String[]> digitToLetterMap = new HashMap<>(){{
        put('2',new String[]{"a","b","c"});
        put('3',new String[]{"d","e","f"});
        put('4',new String[]{"g","h","i"});
        put('5',new String[]{"j","k","l"});
        put('6',new String[]{"m","n","o"});
        put('7',new String[]{"p","q","r","s"});
        put('8',new String[]{"t","u","v"});
        put('9',new String[]{"w","x","y","z"});
    }};
    
    private List<String> result;
    
    public List<String> letterCombinations(String digits) {
       if(digits==null || digits.length() == 0) {
            return new ArrayList();
        }
        
        result = Arrays.asList(digitToLetterMap.get(digits.charAt(0)));
        
        int i=1;
        while (i<digits.length()){
            combineTwoDigits(digitToLetterMap.get(digits.charAt(i)));
            i++;
        }       
        return result;
    }
    
    private void combineTwoDigits(String[] letters){        
        List<String> re = new ArrayList<>(result.size() * letters.length);
        for(int i=0;i<result.size();i++){
            for(int j=0;j<b.length;j++){
                StringBuilder sb = new StringBuilder();
                sb.append(result.get(i));
                sb.append(letters[j]);
                re.add(sb.toString());
            }
        }
        result = re;
    }      
}
Published 36 original articles · won praise 8 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_32763643/article/details/104346854