先把digit-letter建个dictionary
Given a digit string, return all possible letter combinations that the number could represent.
public class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if (digits == null) {
return null;
}
HashMap<Character, char[]> map = new HashMap<>();
map.put('0', new char[] {});
map.put('1', new char[] {});
map.put('2', new char[] {'a', 'b', 'c'});
map.put('3', new char[] {'d', 'e', 'f'});
map.put('4', new char[] {'g', 'h', 'i'});
map.put('5', new char[] {'j', 'k', 'l'});
map.put('6', new char[] {'m', 'n', 'o'});
map.put('7', new char[] {'p', 'q', 'r', 's'});
map.put('8', new char[] {'t', 'u', 'v'});
map.put('9', new char[] {'w', 'x', 'y', 'z'});
StringBuilder sb = new StringBuilder();
helper(map, res, sb, digits);
return res;
}
public void helper(Map<Character, char[]> map, List<String> res, StringBuilder sb, String s) {
if (sb.length() == s.length()) {
res.add(sb.toString());
return;
}
for (char c : map.get(s.charAt(sb.length()))) {
sb.append(c);
helper(map, res, sb, s);
sb.deleteCharAt(sb.length() - 1);
}
}
}