Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:
Input: "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.
[code lang="java"]
class Solution {
public List<String> letterCombinations(String digits) {
}
}
[code]
Idea – 1
If there are n digits each having m possible substitutions, there are
[code lang="java"]
class Solution {
private static final String[] map =
{
"",
"",
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz" // 9
};
public List<String> letterCombinations(String digits) {
List<String> ans = new ArrayList<>();
if(digits.isEmpty())
{
return ans;
}
collect(digits, 0, "", ans);
return ans;
}
private void collect(String digits, int i, String buffer, List<String> ans)
{
if(i >= digits.length())
{
ans.add(buffer);
return;
}
char c = digits.charAt(i);
String subst = map[c-'0'];
for(int j = 0; j < subst.length(); ++j)
{
collect(digits, i+1, buffer+subst.charAt(j), ans);
}
}
}
[code]
Runtime: 0 ms, faster than 100.00% of Java online submissions for Letter Combinations of a Phone Number.Memory Usage: 35.1 MB, less than 98.08% of Java online submissions for Letter Combinations of a Phone Number.