VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-possible-words-phone-digits/

⇱ Print all possible words from phone digits - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all possible words from phone digits

Last Updated : 30 Sep, 2025

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.

👁 frame_3137

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.

[Approach] Using Recursion and Backtracking - O(4^n) Time and O(n) Space

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.


Output
pt pu pv qt qu qv rt ru rv st su sv 
Comment