1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
var letterCombinations = function (digits) { if (!digits) return []; const map = { 2: "abc", 3: "def", 4: "ghi", 5: "jkl", 6: "mno", 7: "pqrs", 8: "tuv", 9: "wxyz", }; let res = []; function fn(index, path) { if (index === digits.length) { res.push(path); return; } for (const char of map[[digits[index]]]) { fn(index + 1, path + char); } } fn(0, ""); return res; }; console.log(letterCombinations("23"));
|