Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Mapping: 2→abc, 3→def, 4→ghi, 5→jkl, 6→mno, 7→pqrs, 8→tuv, 9→wxyz.
digits = "23"["ad","ae","af","bd","be","bf","cd","ce","cf"]digits = ""[]digits = "2"["a","b","c"]For each digit we branch into 3-4 letters, going digit by digit. The recursion depth equals the number of digits. Total combinations = product of letter counts for each digit (e.g., "23" → 3×3 = 9). Since we're building the string immutably (concatenation), no explicit undo step is needed.
Nested loops only work when you know the number of digits. Recursion handles any length.
Each level of the tree is one digit. At a level, try every letter of that digit, append it, and recurse. When the string is as long as the digits, record it.
One tree level per digit. The branching factor is the number of letters.
Run the animation above on your own input, or watch the 1:10 video explanation of Letter Combinations of a Phone Number with a dry run and Java code.