Letter Combinations of a Phone Number

Backtracking, problem 4 of 7

Letter Combinations of a Phone Number

Medium

LC #17

cartesian productstrings

Not attempted yet

On an old phone keypad each digit from 2 to 9 stands for some letters:

2: abc   3: def   4: ghi   5: jkl
6: mno   7: pqrs  8: tuv   9: wxyz

Given a string digits, return every string of letters it could represent (one letter per digit, in order). Return them in any order. An empty digits gives an empty list.

Example 1

Input: digits = "23"
Output: ["ad","ae","af","bd","be",
         "bf","cd","ce","cf"]

Example 2

Input: digits = ""
Output: []

Example 3

Input: digits = "2"
Output: ["a","b","c"]

Constraints

  • 0 ≤ len(digits) ≤ 4
  • Each character of digits is in '2'–'9'.

Python

Loading draft…

Test results

7 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 4 hidden

Run examples, then submit all tests.