iq.lab
Python starts when a code cell comes near or you run one
mediumBacktracking target 25 min

Letters from phone digits

On a phone keypad, each digit from 2 to 9 sits on a key with three or four letters:

Digit23456789
Lettersabcdefghijklmnopqrstuvwxyz

You get a string digits. Return a list of every string you can make by picking one letter from the key of each digit, keeping the digits' order: the first letter comes from the first digit, the second from the second, and so on.

Return the strings in any order. If digits is empty, return an empty list.

Example 1
Inputdigits = "56"Output["jm", "jn", "jo", "km", "kn", "ko", "lm", "ln", "lo"]

3 letters for 5 (jkl) times 3 letters for 6 (mno) gives 9 strings.

Example 2
Inputdigits = "7"Output["p", "q", "r", "s"]

The key for 7 has four letters.

Example 3
Inputdigits = ""Output[]

No digits, so no strings.

Constraints
  • 0 ≤ len(digits) ≤ 4

  • Each character of digits is a digit from 2 to 9.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.