Problem 538
Letter Combinations of a Phone Number
Medium · 20 pts · String, Backtracking
Problem
On a 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 and 9 = wxyz. Given a string of such digits, list every letter string it could represent, choosing one letter for each digit in order. Print them in lexicographic order on one line, separated by spaces. Building them digit by digit, in keypad order, produces exactly this order.
Input
A single string D of digits from 2 to 9.
Output
Print all combinations in lexicographic order, separated by spaces.
Constraints
1 <= |D| <= 5
Example input
23
Example output
ad ae af bd be bf cd ce cf
Explanation
For 23, the digit 2 gives a, b or c and 3 gives d, e or f. The 9 combinations in order are ad ae af bd be bf cd ce cf.