Letter Combinations of a Phone Number
Difficulty: Medium
On a phone keypad, each digit from 2 to 9 has a few letters (2 -> abc, 3 -> def, and so on). Given a string of digits, return every letter combination the digits could represent - one letter per digit, in order.
If the input is empty, return no combinations.
Examples
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
2 maps to a/b/c and 3 maps to d/e/f, so every pairing of one letter from each gives 3 x 3 = 9 combinations.
Input: digits = ""
Output: []
No digits means no combinations at all.
Input: digits = "2"
Output: ["a","b","c"]
A single digit just gives its own letters, one per combination.
Constraints
0 <= digits.length <= 4
Each character of digits is between "2" and "9".
Approach
Each digit adds exactly one of its letters, so the answer is every way to pick one letter per digit, in order.
One way builds level by level: start with a list holding the empty string. For each digit, replace the list with every partial combination extended by each of that digit's letters. After the last digit, the list holds every combination.
A more memory-efficient way builds one combination at a time instead of keeping a whole intermediate list. Pick a letter for the current digit, move to the next, and record the combination once every digit has a letter. Then back up and try the next letter for the last digit - the same choose/explore/undo rhythm, working through digits instead of array positions.
Solutions
Iterative — Build Up Combinations Level by Level
Keep a list of partial combinations, starting with the empty string. For each digit, build a new list by appending each of its letters to every partial combination. After the last digit, the list holds every combination.
function letterCombinations(digits) {
if (digits.length === 0) return [];
const digitToLetters = {
"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
"6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",
};
let combinations = [""];
for (const digit of digits) {
const letters = digitToLetters[digit];
const next = [];
for (const combo of combinations) {
for (const letter of letters) {
next.push(combo + letter);
}
}
combinations = next;
}
return combinations;
}Time: O(n * 4^n), where n is the number of digits · Space: O(n * 4^n) for the intermediate and final combination lists
Optimal — Backtracking, One Combination at a Time
Build one combination in a running string, digit by digit. At each digit, try each of its letters: append it, recurse into the next digit, then remove it. Record the string once every digit has a letter.
function letterCombinations(digits) {
if (digits.length === 0) return [];
const digitToLetters = {
"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
"6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",
};
const result = [];
let current = "";
function backtrack(index) {
if (index === digits.length) {
result.push(current);
return;
}
const letters = digitToLetters[digits[index]];
for (const letter of letters) {
current += letter;
backtrack(index + 1);
current = current.slice(0, -1); // undo
}
}
backtrack(0);
return result;
}Time: O(n * 4^n) · Space: O(n) auxiliary (recursion depth + the running string), plus output storage