![]() |
VOOZH | about |
Given an array arr[] representing the numbers of a keypad, find all possible words in any order which can be generated by pressing numbers from the array.
A mapping of digits to letters (just like on the telephone buttons) is being followed.
Note: Number 0 and 1 do not map to any letters.
Examples:
Input: arr[] = [5]
Output: [j, k, l]
Explanation: The digit 5 maps to letters j, k, l; each letter forms a word of length 1.Input: arr[] = [7, 8]
Output: [pt, pu, pv, qt, qu, qv, rt, ru, rv, st, su, sv]
Explanation: Digit 7 maps to p, q, r, s and 8 maps to t, u, v; all words are formed by picking one letter from each digit sequentially.
The idea is to use recursion to build all possible words step by step. Each digit maps to certain letters, and at every step we choose one letter from the current digit and add it to the growing string (prefix). Then we move to the next digit and repeat the process. The recursion stops when we have processed all digits at this point, the formed string is a valid combination, so we add it to the result. If a digit does not map to any letters (like 0 or 1), we simply skip it and move forward.
pt pu pv qt qu qv rt ru rv st su sv