Problem 555040 · medium · Phase 05 Advanced Algorithms & Graphs

Letters on a Code Dial

backtracking · strings · cartesian product

An old safe has a numeric dial, and each digit stands for a small group of letters:

'1': "xy"    '2': "abc"   '3': "de"    '4': "fgh"   '5': "ijk"
'6': "lm"    '7': "nop"   '8': "qrs"   '9': "tuv"   '0': "wz"

Given the dialled string code (digits only), return every word that could be spelled by choosing one letter for each digit, in any order. For an empty code return an empty list.

Examples

Input:  code = "13"
Output: ["xd", "xe", "yd", "ye"]

Input:  code = "0"
Output: ["w", "z"]

Input:  code = ""
Output: []

Constraints

  • 0 <= len(code) <= 6
  • At most 3^6 = 729 words.

Goals

  • Map each input symbol to a set of choices and recurse position by position
  • Handle the empty input as a special case
Starting Python…