Leetcode depth first search letter combination of phone number java

Title description
Given a string containing only numbers 2-9, return all letter combinations that it can represent.
The mapping of numbers to letters is given as follows (same as phone buttons). Note that 1 does not correspond to any letters.
Insert picture description here
Method 1: Backtracking to
initialize a string, the initial value is empty, each time one digit of the phone number is taken, all letters corresponding to the number are obtained from the hash table, and one of the letters is inserted after the existing alphabetical arrangement , And then continue to process the last digit of the phone number until all digits are processed, and then go back and traverse other letter combinations.

class Solution {
    
    
    public List<String> letterCombinations(String digits) {
    
    
        List<String> res = new ArrayList<>();
        if(digits.length() == 0){
    
    
            return res;
        }
        Map<Integer,String> map = new HashMap<>();
        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");
        back(res,map,digits,0,new StringBuffer());
        return res;
    }
    public void back(List<String> lists,Map<Integer,String> map,String digits,int index,StringBuffer list){
    
    
       if(index == digits.length()){
    
    
           lists.add(list.toString());
       }else{
    
    
           String s = map.get(digits.charAt(index)-'0');
           int strlen = s.length();
           for(int i = 0;i<strlen;i++){
    
    
               list.append(s.charAt(i));
               back(lists,map,digits,index+1,list);
               list.deleteCharAt(index);
           }
       }
    }
}

Method 2:
Example: "23"
loops through the string "23": the
first time loop ("2"): add the letters corresponding to "2" to the
list (abc) and the list is a, b, c
If it is not the first cycle ("3"), the first element (a) in the list will pop up, and the letters corresponding to "3" will be added after "a" and added to the list.
At this time, the list is: b ,c,ad,ae,af
Next time, pop up b, add the letters corresponding to "3" after "b" in turn, and add them to the list.
At this time, the list is: c, ad, ae, af, bd, be,
Next time bf , pop up c, and add letters corresponding to "3" after "c" and add them to the list.
At this time, the list is: ad, ae, af, bd, be, bf, cd, ce, cf

class Solution {
    
    
    public List<String> letterCombinations(String digits) {
    
    
        List<String> list = new ArrayList<String>();
        if(digits.length() == 0){
    
    
            return list;
        }
        Map<Integer,String> dictionary = new HashMap<>();
        dictionary.put(2,"abc");
        dictionary.put(3,"def");
        dictionary.put(4, "ghi");
        dictionary.put(5, "jkl");
        dictionary.put(6, "mno");
        dictionary.put(7, "pqrs");
        dictionary.put(8, "tuv");
        dictionary.put(9, "wxyz");
        for(int i = 0;i<digits.length();i++){
    
    
            char c = digits.charAt(i);
            String s = dictionary.get(c-'0');
            if(i == 0){
    
    
                for(int j = 0;j<s.length();j++){
    
    
                    list.add(s.substring(j,j+1));
                }
            }else{
    
    
                int listSize = list.size();
                for(int k = 0;k<listSize;k++){
    
    
                    String old = list.remove(0);
                    for(int p = 0;p < s.length();p++){
    
    
                        list.add(old+s.substring(p,p+1));
                    }
                }
            }
        }
        return list;
    }
}

Guess you like

Origin blog.csdn.net/stonney/article/details/110678060