Skip to content
StackDecode
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

  1. Each phone digit stands for 3–4 letters.
  2. Take the digits one at a time. For the current digit try each of its letters in turn.
  3. When every digit has a letter, the string is one combination. Record it.
  4. Backtrack to change the most recent letter. The number of results is the product of the letter counts.

Watch it run

Loading animation…