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 List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<>();
String[] dic = new String[]{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
Combinations(digits,0,result,"",dic);
return result;
}
private void Combinations(String digits,int index,List<String> result,String curr,String[] dic){
if (index==digits.length()){
if (curr.length()!=0) result.add(curr);
return;
}
String temp = dic[digits.charAt(index)-'0'];
for (int i=0;i<temp.length();i++){
String next = curr + temp.charAt(i);
Combinations(digits,index+1,result,next,dic);
}
}