Editor

Phone Keypad Words

#274 · Recursion and backtracking · Intermediate · 60 XP · about 12 min

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

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

A string of digits can therefore spell many letter strings, with one letter for each digit, in the same order. Write keypad_words(digits) that returns every letter string the digits can spell, as a list sorted alphabetically. If digits is empty, return an empty list.

Examples

keypad_words("26")  # returns ['am', 'an', 'ao', 'bm', 'bn', 'bo', 'cm', 'cn', 'co']
keypad_words("9")   # returns ['w', 'x', 'y', 'z']
keypad_words("")    # returns []

Constraints

  • 0 <= len(digits) <= 8
  • digits contains only the characters 2 to 9.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.