1. All digits used
A combination is complete once every digit has contributed a letter.
if (index === digits.length)Typorithm · medium
Step 1 of 8 · intro card
Backtracking
Map each phone digit to its letters and build every combination by choosing one letter per digit.
Each digit from 2 to 9 maps to a group of letters, like a phone keypad.
Backtracking advances digit by digit, appending each letter of the current digit's group and recursing.
Once index reaches the digit count, the path holds one full-length combination to record.
Map digits to letter groups.
Recurse one digit deeper per letter.
Record full-length combinations.
function letterCombinations(digits) {
if (digits.length === 0) return [];
const map = {
2: "abc",
3: "def",
4: "ghi",
5: "jkl",
6: "mno",
7: "pqrs",
8: "tuv",
9: "wxyz",
};
const result = [];
function backtrack(index, path) {
if (index === digits.length) {
result.push(path.join(""));
return;
}
const letters = map[digits[index]];
for (const letter of letters) {
path.push(letter);
backtrack(index + 1, path);
path.pop();
}
}
backtrack(0, []);
return result;
}Connect each part of the code to the concept before you type.
A combination is complete once every digit has contributed a letter.
if (index === digits.length)Map the current digit to its group of letters.
const letters = map[digits[index]]Branch on every letter the current digit offers.
for (const letter of letters)Append the chosen letter to the growing combination.
path.push(letter)Remove the letter to try the next one in the group.
path.pop()Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputPhone digits 23
∅ Empty state
Follow temporary values, local state and supporting structures at this step.
Live recursive calls, including their arguments. Returning releases the top frame.
Current choice path0 entries
Local state · indices and saved values
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state