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.
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 Solution {
public String []c={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public int []total={0,0,3,3,3,3,3,4,3,4};
public List<String> letterCombinations(String digits) {
List<String> res=new LinkedList<String>();
dfs(digits,"",0,res);
return res;
}
public void dfs(String digits,String ans,int index,List<String> res){
if(index==digits.length()){
res.add(ans);
return;
}
int i=digits.charAt(index)-'0';
if(i<2 || i>10) return;
for(int j=0;j<total[i];j++){
dfs(digits,ans+String.valueOf(c[i].charAt(j)),index+1,res);
}
}
}