BacktrackingAnimated
Letter Combinations of a Phone Number
Choose one letter for each digit.
Time
O(4ⁿ)
Extra space
O(n)
Use it for
T9-style expansion.
How it works
- Each phone digit stands for 3–4 letters.
- Take the digits one at a time. For the current digit try each of its letters in turn.
- When every digit has a letter, the string is one combination. Record it.
- Backtrack to change the most recent letter. The number of results is the product of the letter counts.
Watch it run
Loading animation…