DFS
public List<String> letterCombinations(String digits) {
List<String> result = new ArrayList<String>();
if (digits == null || digits.length() == 0) {
result.add("");
return result;
}
String[] map = {"abc","def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
//DFS
for (String rest : letterCombinations(digits.substring(1))){
for (char c : map[digits.charAt(0) - '0' - 2].toCharArray()) {
result.add(c + rest);
}
}
return result;
}
No comments:
Post a Comment