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:
| Digit | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|
| Letters | abc | def | ghi | jkl | mno | pqrs | tuv | wxyz |
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
Input
digits = "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
Input
digits = "7"Output["p", "q", "r", "s"]The key for 7 has four letters.
Example 3
Input
digits = ""Output[]No digits, so no strings.
Constraints
0 ≤ len(digits) ≤ 4
Each character of
digitsis 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.
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.