package me.wayne.leetcodetop100.backtracking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 给定一个仅包含数字2-9的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 * <p>
 * https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
 */
public class LC17_LetterCombinations {
    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits == null || digits.length() == 0) {
            return res;
        }

        Map<Character, String> digitToLetterMap = new HashMap<>();
        digitToLetterMap.put('2', "abc");
        digitToLetterMap.put('3', "def");
        digitToLetterMap.put('4', "ghi");
        digitToLetterMap.put('5', "jkl");
        digitToLetterMap.put('6', "mno");
        digitToLetterMap.put('7', "pqrs");
        digitToLetterMap.put('8', "tuv");
        digitToLetterMap.put('9', "wxyz");

        StringBuilder path = new StringBuilder();
        //递归
        backTracking(path, digitToLetterMap, digits, 0, res);
        return res;
    }

    private void backTracking(StringBuilder path, Map<Character, String> map, String digits,
            int dept, List<String> res) {
        if (digits.length() == dept) {
            res.add(path.toString());
            return;
        }

        char digit = digits.charAt(dept);
        String letters = map.get(digit);
        for (int i = 0; i < letters.length(); i++) {
            path.append(letters.charAt(i));
            backTracking(path, map, digits, dept + 1, res);
            path.deleteCharAt(dept);
        }
    }
}

@@ -64,24 +64,54 @@
 .ArrayList;%0A
+import java.util.Collections;%0A
 import java.
@@ -460,16 +460,17 @@
 tions %7B%0A
+%0A
     publ
@@ -533,76 +533,12 @@
     
-List%3CString%3E res = new ArrayList%3C%3E();%0A        if (digits == null %7C%7C 
+if (
 digi
@@ -572,27 +572,47 @@
      return 
-res
+Collections.emptyList()
 ;%0A        %7D%0A
@@ -1030,90 +1030,58 @@
     
+List%3C
 String
-Builder path = new StringBuilder();%0A        //%E9%80%92%E5%BD%92%0A        backTracking(path, 
+%3E ans = new ArrayList%3C%3E();%0A        dfs(
 digi
@@ -1093,16 +1093,21 @@
 tterMap,
+ ans,
  digits,
@@ -1110,19 +1110,35 @@
 its, 0, 
-res
+new StringBuilder()
 );%0A     
@@ -1147,18 +1147,18 @@
  return 
-re
+an
 s;%0A    %7D
@@ -1180,122 +1180,114 @@
 oid 
-backTracking(StringBuilder path, Map%3CCharacter, String%3E map, String digits,%0A            int dept, List%3CString%3E res
+dfs(Map%3CCharacter, String%3E map, List%3CString%3E ans, String digits, int depth,%0A            StringBuilder path
 ) %7B%0A
@@ -1290,38 +1290,36 @@
 ) %7B%0A        if (
-digits
+path
 .length() == dep
@@ -1316,19 +1316,30 @@
 h() == d
-ept
+igits.length()
 ) %7B%0A    
@@ -1346,18 +1346,18 @@
         
-re
+an
 s.add(pa
@@ -1416,21 +1416,18 @@
    char 
-digit
+ch
  = digit
@@ -1431,32 +1431,33 @@
 gits.charAt(dept
+h
 );%0A        Strin
@@ -1476,21 +1476,18 @@
 map.get(
-digit
+ch
 );%0A     
@@ -1594,30 +1594,20 @@
     
-backTracking(path, map
+dfs(map, ans
 , di
@@ -1620,17 +1620,19 @@
 dept
+h
  + 1, 
-res
+path
 );%0A 
@@ -1660,20 +1660,33 @@
 eCharAt(
-dept
+path.length() - 1
 );%0A     

refactor LC17_LetterCombinations