Given a string of digits 2–9, return all letter combinations the number could represent on a phone keypad, in any order.
Pick a letter for the current digit, recurse on the next digit, then undo. When every digit has a letter, record the string.
1function letterCombinations(digits: string): string[] {2if (!digits) return [];3const keys = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];4const res: string[] = [], path: string[] = [];5function dfs(i: number) {6if (i === digits.length) { res.push(path.join("")); return; }7for (const c of keys[+digits[i]]) {8path.push(c);9dfs(i + 1);10path.pop();11}12}13dfs(0);14return res;15}
Digit 2 → 'a'.
Space: play/pause · ←/→: step