function letterCombinations(digits: string): string[] {
if (digits.length === 0) {
return [];
}
type mapKeys = keyof typeof map;
const ans: string[] = [];
const map = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
};
if (digits.length === 1) {
return map[digits as mapKeys].split("");
}
const handleRes = (curStr: string, i: number) => {
if (i > digits.length - 1) {
ans.push(curStr);
return;
}
const letters = map[digits[i] as mapKeys];
for (const letter of letters) {
handleRes(curStr + letter, i + 1);
}
};
handleRes("", 0);
return ans;
}