Find all possible combinations of words formed from mobile keypad

The standard 12-key telephone keypad looks like the following:

keypad

To write text using the keypad you have to press the number keys. For example, to write X press the key 9 twice. Given a sequence of numbers, return all possible combinations of words formed from mobile keypad which have some digits associated with each key.

Example Solution

When the solution is called with arr = [2, 3, 4] the expectected list of permutations is the following:

ADG BDG CDG AEG BEG CEG AFG BFG CFG ADH BDH CDH AEH BEH CEH AFH BFH CFH ADI BDI CDI AEI BEI CEI AFI BFI CFI

Examples:

  • ADG is, press 2 once, press 3 once, press 4 once.
  • BDG is, press 2 twice, press 3 once, press 4 once.

Contributed by Murat Sütunç