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.
java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public List<String> letterCombinations(String digits) {
LinkedList<String> ans = new LinkedList<>();
if (0 == digits.length()) {
return ans;
}
String[] mapping = new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.add("");
for (int i = 0; i < digits.length(); i++) {
int x = Character.getNumericValue(digits.charAt(i));
while (ans.peek().length() == i) {
String temp = ans.remove();
for (Character c : mapping[x].toCharArray()) {
ans.add(temp + c);
}
}
}
return ans;
}
This alogirthm uses a queque to make sure every new character was added to the previous string, shown as below.