Skip to content
BytePatterns

Phone Keypad Words

MediumBacktracking#backtracking#decision-tree~20m

Problem

An old phone keypad maps each digit to a small group of letters: 2 to abc, 3 to def, 4 to ghi, 5 to jkl. Given a string of those digits, return every letter string it could have been typed as. Order the results by taking each digit's letters left to right. An empty digit string produces no words at all.

Examples

Input:  digits = "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Why:    three letters on 2 times three letters on 3
Input:  digits = "4"
Output: ["g", "h", "i"]
Why:    a single digit contributes one letter per word
Input:  digits = ""
Output: []
Why:    edge case, no digits means no word, not one empty word

Hints

0 / 3

Stuck on the idea rather than the code? The Decision Tree covers it.