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.
[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.
#include <iostream>
#include <vector>
using namespace std;
// Recursive function to generate combinations
void possibleWordsRec(vector<int> &arr, int index, string &prefix,
vector<string> &padMap, vector<string> &res) {
if (index == arr.size()) {
res.push_back(prefix);
return;
}
int digit = arr[index];
// Skip invalid digits
if (digit < 2 || digit > 9) {
possibleWordsRec(arr, index + 1, prefix, padMap, res);
return;
}
// Place all possible letters for this digit
for (char ch : padMap[digit]) {
prefix.push_back(ch);
possibleWordsRec(arr, index + 1, prefix, padMap, res);
// Backtracking to previous state
prefix.pop_back();
}
}
// Function to find all possible letter combinations
vector<string> possibleWords(vector<int> &arr) {
vector<string> res;
// mapping numbers with letters
vector<string> padMap = {"", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};
string prefix = "";
possibleWordsRec(arr, 0, prefix, padMap, res);
return res;
}
int main() {
vector<int> arr = {7, 8};
vector<string> words = possibleWords(arr);
for (string word : words)
cout << word << " ";
return 0;
}
import java.util.ArrayList;
class GFG {
// Recursive function to generate combinations
static void possibleWordsRec(int[] arr, int index, StringBuilder prefix,
String[] padMap, ArrayList<String> res) {
if (index == arr.length) {
res.add(prefix.toString());
return;
}
int digit = arr[index];
// Skip invalid digits
if (digit < 2 || digit > 9) {
possibleWordsRec(arr, index + 1, prefix, padMap, res);
return;
}
// Place all possible letters for this digit
for (char ch : padMap[digit].toCharArray()) {
prefix.append(ch);
possibleWordsRec(arr, index + 1, prefix, padMap, res);
// Backtracking to previous state
prefix.deleteCharAt(prefix.length() - 1);
}
}
// Function to find all possible letter combinations
static ArrayList<String> possibleWords(int[] arr) {
ArrayList<String> res = new ArrayList<>();
// mapping numbers with letters
String[] padMap = {"", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};
StringBuilder prefix = new StringBuilder();
possibleWordsRec(arr, 0, prefix, padMap, res);
return res;
}
public static void main(String[] args) {
int[] arr = {7, 8};
ArrayList<String> words = possibleWords(arr);
for (String word : words) {
System.out.print(word + " ");
}
}
}
# Recursive function to generate combinations
def possibleWordsRec(arr, index, prefix, padMap, res):
if index == len(arr):
res.append(prefix)
return
digit = arr[index]
# Skip invalid digits
if digit < 2 or digit > 9:
possibleWordsRec(arr, index + 1, prefix, padMap, res)
return
# Place all possible letters for this digit
for ch in padMap[digit]:
possibleWordsRec(arr, index + 1, prefix + ch, padMap, res)
# Function to find all possible letter combinations
def possibleWords(arr):
res = []
# mapping numbers with letters
padMap = ["", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"]
possibleWordsRec(arr, 0, "", padMap, res)
return res
if __name__ =="__main__":
arr = [7, 8]
words = possibleWords(arr)
for word in words:
print(word, end=" ")
using System;
using System.Collections.Generic;
class GFG {
// Recursive function to generate combinations
static void possibleWordsRec(int[] arr, int index, string prefix,
string[] padMap, List<string> res) {
if (index == arr.Length) {
res.Add(prefix);
return;
}
int digit = arr[index];
// Skip invalid digits
if (digit < 2 || digit > 9) {
possibleWordsRec(arr, index + 1, prefix, padMap, res);
return;
}
// Place all possible letters for this digit
foreach (char ch in padMap[digit]) {
possibleWordsRec(arr, index + 1, prefix + ch, padMap, res);
}
}
// Function to find all possible letter combinations
static List<string> possibleWords(int[] arr) {
List<string> res = new List<string>();
// mapping numbers with letters
string[] padMap = { "", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz" };
possibleWordsRec(arr, 0, "", padMap, res);
return res;
}
static void Main() {
int[] arr = { 7, 8 };
List<string> words = possibleWords(arr);
foreach (string word in words)
{
Console.Write(word + " ");
}
}
}
// Recursive function to generate combinations
function possibleWordsRec(arr, index, prefix, padMap, res) {
if (index === arr.length) {
res.push(prefix);
return;
}
let digit = arr[index];
// Skip invalid digits
if (digit < 2 || digit > 9) {
possibleWordsRec(arr, index + 1, prefix, padMap, res);
return;
}
// Place all possible letters for this digit
for (let ch of padMap[digit]) {
possibleWordsRec(arr, index + 1, prefix + ch, padMap, res);
}
}
// Function to find all possible letter combinations
function possibleWords(arr) {
let res = [];
// mapping numbers with letters
let padMap = ["", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"];
possibleWordsRec(arr, 0, "", padMap, res);
return res;
}
// Driver Code
let arr = [7, 8];
let words = possibleWords(arr);
for (let word of words) {
process.stdout.write(word + " ");
}
Output
pt pu pv qt qu qv rt ru rv st su sv