package Level3;
import java.util.ArrayList;
/**
* 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.
https://www.cppentry.com/upload_files/article/76/1_mftcv__.png
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.
*
*/
public class S17 {
public static void main(String[] args) {
System.out.println(letterCombinations("23"));
}
public static ArrayList
letterCombinations(String digits) {
String[] ss = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ArrayList ret = new ArrayList();
rec(ret, digits.length(), ss, digits, new StringBuffer());
return ret;
}
public static void rec(ArrayList ret, int remain, String[] ss, String digits, StringBuffer sb){
// 说明对digits遍历结束
if(remain == 0){
ret.add(sb.toString());
return;
}
String s = ss[digits.charAt(0)-'0']; // 得到digits[0]对应的string s
// DFS
for(int i=0; i