17. 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.
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
dfs
public class Solution {
List<String> res = new ArrayList<>();
String[] dict = new String[]{"","", "abc", "def", "ghi", "jkl", "mno","pqrs","tuv", "wxyz"};
public List<String> letterCombinations(String digits) {
combination(digits, 0, "");
return res;
}
private void combination(String digits, int k, String current){
if(k == digits.length()){
if(!current.isEmpty())res.add(current);
return;
}
String s = dict[digits.charAt(k)- '0'];
for(Character c : s.toCharArray()){
combination(digits, k+1, current+c);
}
}
}