题解
const phoneMap = {
'2': "abc",
'3': "def",
'4': "ghi",
'5': "jkl",
'6': "mno",
'7': "pqrs",
'8': "tuv",
'9': "wxyz"
};
var letterCombinations = function(digits) {
if (digits.length === 0) {
return [];
}
const combinations = [];
backtrack(combinations, digits, 0, "");
return combinations;
};
function backtrack(combinations, digits, index, combination) {
if (index === digits.length) {
combinations.push(combination);
return;
}
const digit = digits[index];
const letters = phoneMap[digit];
for (let i = 0; i < letters.length; i++) {
backtrack(combinations, digits, index + 1, combination + letters[i]);
}
}