03. Possible Words From Phone Digits
β GFG solution to the Possible Words From Phone Digits problem: generate all possible letter combinations from phone keypad digits using iterative and backtracking approaches. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a keypad (as shown in the diagram) and an array arr[]
containing digits. Your task is to list all possible words in any order which can be generated by pressing numbers in arr[]
sequentially.
Note: Number 0 and 1 do not map to any letters. You can return the words in any order, the driver code will print them in sorted order.
π Examples
Example 1
Input: arr[] = [2, 3]
Output: [ad, ae, af, bd, be, bf, cd, ce, cf]
Explanation: When we press 2 and 3 total 3 x 3 = 9 possible words formed.
Example 2
Input: arr[] = [2]
Output: [a, b, c]
Explanation: When we press 2 total 3 possible words formed.
π Constraints
$1 \le \text{arr.size()} \le 9$
$0 \le \text{arr}[i] \le 9$
β
My Approach
The optimal approach uses an Iterative Build-Up technique to generate all possible letter combinations:
Iterative Cross-Product Generation
Initialize:
Create a keypad mapping array where index represents digit and value contains corresponding letters.
Start with a result vector containing an empty string
[""]
.
Process Each Digit:
For each digit in the input array, skip if it's 0 or 1 (no letter mapping).
Create a temporary vector to store new combinations.
Generate Combinations:
For each existing string in the result vector, append each possible character from the current digit's letter mapping.
Use cross-product logic: if current result has
m
strings and digit hask
letters, generatem Γ k
new combinations.
Update Result:
Replace the result vector with the newly generated combinations using move semantics for efficiency.
Return Final Result:
After processing all digits, return the complete list of combinations.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(4^n Γ n), where n is the number of valid digits (2-9) in the array. In the worst case, each digit can map to 4 characters (like 7 and 9), and we generate 4^n combinations. Each combination takes O(n) time to build as we concatenate characters.
Expected Auxiliary Space Complexity: O(4^n Γ n), as we store all possible combinations in the result vector. Each combination string has length up to n characters.
π§βπ» Code (C++)
class Solution {
public:
vector<string> possibleWords(vector<int> &arr) {
vector<string> res = {""};
string keys[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for (int d : arr) {
if (d < 2 || d > 9) continue;
vector<string> temp;
for (string &s : res)
for (char c : keys[d])
temp.push_back(s + c);
res = move(temp);
}
return res;
}
};
β Code (Java)
class Solution {
public ArrayList<String> possibleWords(int[] arr) {
ArrayList<String> res = new ArrayList<>();
res.add("");
String[] keys = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for (int d : arr) {
if (d < 2 || d > 9) continue;
ArrayList<String> temp = new ArrayList<>();
for (String s : res)
for (char c : keys[d].toCharArray())
temp.add(s + c);
res = temp;
}
return res;
}
}
π Code (Python)
class Solution:
def possibleWords(self, arr):
res = [""]
keys = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
for d in arr:
if d < 2 or d > 9: continue
res = [s + c for s in res for c in keys[d]]
return res
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated