Loading...
Loading...
Old keypad phones, like the one Kavya's grandfather still uses, have letters on the number keys: 2 has abc, 3 has def, 4 has ghi, 5 has jkl, 6 has mno, 7 has pqrs, 8 has tuv and 9 has wxyz.
Kavya types a short code of digits. Print every word the code could stand for, choosing one letter of the key for each digit. Print the words in alphabetical order, one per line.
Example 1
Input
23
Output
ad ae af bd be bf cd ce cf
3 letters for 2 and 3 letters for 3 give 9 words, from ad to cf.
Example 2
Input
7
Output
p q r s
Key 7 has four letters.
Try the problem first. Open one hint at a time.
Each digit adds one letter to the word. The first digit chooses the first letter, and so on.
Write a method that takes the position in the code and the word built so far. When the position reaches the end, print the word.
At each position, loop over the letters of that digit's key: add one, go to the next position, then remove it.
Keep the keys in a String[] indexed by digit, and get the index with code.charAt(pos) - '0'.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor