Iterative Letter Combinations of a Phone Number
Last Updated :
11 Jul, 2025
Given an integer array arr[] containing digits from [0, 9], the task is to print all possible letter combinations that the numbers could represent. A mapping of digits to letters (just like on the telephone buttons) is being followed. Note that 0 and 1 do not map to any letters. All the mapping are shown in the image below:
Examples:
Input: arr[] = [2, 3]
Output: ad ae af bd be bf cd ce cf
Explanation: When we press 2,3 then ad, ae, af, bd, ... cf are the list of possible words.
Input: arr[] = [5]
Output: j k l
Explanation: When we press 5 then j, k, l are the list of possible words.
Approach:
The idea behind this problem is to generate all possible letter combinations for a given sequence of digits based on a traditional phone keypad mapping. Each digit from 2 to 9 corresponds to a set of letters (for example, 2 maps to 'abc', 3 maps to 'def', and so on). The goal is to combine these letters for the given sequence of digits to form all possible words.
We can solve this using an iterative approach with a queue. Start by initializing the queue with an empty string. For each digit in the input array, we extend each string in the queue by appending all the possible letters for that digit. We repeat this until we’ve processed all digits and generated all possible combinations. The queue ensures that we build combinations in the correct order.
C++
// C++ implementation to print all possible
// letter combinations using Queue
#include <bits/stdc++.h>
using namespace std;
vector<string> possibleWords(vector<int> &arr) {
// Mapping digits to corresponding letters
vector<string> mp =
{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> result;
// Queue to store intermediate combinations
queue<string> q;
q.push("");
while (!q.empty()) {
// Get the front string from the queue
string prefix = q.front();
q.pop();
// Check if the current string is complete
if (prefix.length() == arr.size()) {
result.push_back(prefix);
}
else {
// Get the corresponding digit
int digit = arr[prefix.length()];
// Skip invalid digits
if (digit < 2 || digit > 9) {
continue;
}
// Add all possible letters for this digit
for (char letter : mp[digit]) {
q.push(prefix + letter);
}
}
}
return result;
}
void printArr(vector<string> &words) {
for (string word : words) {
cout << word << " ";
}
cout << endl;
}
int main() {
vector<int> arr = {2, 3};
vector<string> words = possibleWords(arr);
printArr(words);
return 0;
}
Java
// Java implementation to print all possible
// letter combinations using Queue
import java.util.*;
class GfG {
// Method to get all possible words
static String[] possibleWords(int[] arr) {
// Mapping digits to corresponding letters
String[] mp
= { "", "", "abc", "def", "ghi",
"jkl", "mno", "pqrs", "tuv", "wxyz" };
// List to store the result
List<String> result = new ArrayList<>();
// Queue to store intermediate combinations
Queue<String> q = new LinkedList<>();
q.add("");
while (!q.isEmpty()) {
// Get the front string from the queue
String prefix = q.poll();
// Check if the current string is complete
if (prefix.length() == arr.length) {
result.add(prefix);
}
else {
// Get the corresponding digit
int digit = arr[prefix.length()];
// Skip invalid digits
if (digit < 2 || digit > 9) {
continue;
}
// Add all possible letters for this digit
for (char letter :
mp[digit].toCharArray()) {
q.add(prefix + letter);
}
}
}
return result.toArray(new String[0]);
}
static void printArr(String[] words) {
for (String word : words) {
System.out.print(word + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = { 2, 3 };
String[] words = possibleWords(arr);
printArr(words);
}
}
Python
# Python implementation to print all possible
# letter combinations using Queue
from collections import deque
def possibleWords(arr):
# Mapping digits to corresponding letters
mp = ["", "", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz"]
# List to store the result
result = []
# Queue to store intermediate combinations
q = deque()
# Start with an empty string
q.append("")
while q:
# Get the front string from the queue
prefix = q.popleft()
# Check if the current string is complete
if len(prefix) == len(arr):
result.append(prefix)
else:
# Get the corresponding digit
digit = arr[len(prefix)]
# Skip invalid digits
if digit < 2 or digit > 9:
continue
# Add all possible letters for this digit
for letter in mp[digit]:
q.append(prefix + letter)
return result
def printArr(words):
print(" ".join(words))
if __name__ == "__main__":
arr = [2, 3]
words = possibleWords(arr)
printArr(words)
C#
// C# implementation to print all possible
// letter combinations using Queue
using System;
using System.Collections.Generic;
class GfG {
// Method to get all possible words
static string[] PossibleWords(int[] arr) {
// Mapping digits to corresponding letters
string[] mp
= { "", "", "abc", "def", "ghi",
"jkl", "mno", "pqrs", "tuv", "wxyz" };
// List to store the result
List<string> result = new List<string>();
// Queue to store intermediate combinations
Queue<string> q = new Queue<string>();
// Start with an empty string
q.Enqueue("");
while (q.Count > 0) {
// Get the front string from the queue
string prefix = q.Dequeue();
// Check if the current string is complete
if (prefix.Length == arr.Length) {
result.Add(prefix);
}
else {
// Get the corresponding digit
int digit = arr[prefix.Length];
// Skip invalid digits
if (digit < 2 || digit > 9) {
continue;
}
// Add all possible letters for this digit
foreach(char letter in mp[digit]) {
q.Enqueue(prefix + letter);
}
}
}
return result.ToArray();
}
static void PrintArr(string[] words) {
foreach(string word in words) {
Console.Write(word + " ");
}
Console.WriteLine();
}
static void Main() {
int[] arr = { 2, 3 };
string[] words = PossibleWords(arr);
PrintArr(words);
}
}
JavaScript
// Javascript implementation to print all possible
// letter combinations using Queue
function possibleWords(arr) {
// Mapping digits to corresponding letters
const mp = [
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs",
"tuv", "wxyz"
];
// Array to store the result
let result = [];
// Queue to store intermediate combinations
let q = [];
q.push("");
while (q.length > 0) {
// Get the front string from the queue
let prefix = q.shift();
// Check if the current string is complete
if (prefix.length === arr.length) {
result.push(prefix);
}
else {
// Get the corresponding digit
let digit = arr[prefix.length];
// Skip invalid digits
if (digit < 2 || digit > 9) {
continue;
}
// Add all possible letters for this digit
for (let letter of mp[digit]) {
q.push(prefix + letter);
}
}
}
return result;
}
function printArr(words) { console.log(words.join(" ")); }
//Driver code
let arr = [ 2, 3 ];
let words = possibleWords(arr);
printArr(words);
Outputad ae af bd be bf cd ce cf
Time Complexity: O(4^n), where n is the length of the input array arr[], as each digit can map to a maximum of 4 letters (e.g., digit '7' maps to 'pqrs').
Auxiliary Space: O(4^n), due to the space used by the queue and the result array, which store all possible combinations.
Related articles:
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem