AfterAcademy Tech
•
12 Oct 2020

Difficulty: Hard
Asked in: Google
Problem Description
Given a string str, containing digits from 2 - 9 inclusive, write a program to return all the possible letter combinations that the number could represent.
Problem Note:

Example
Input: "24"
Output: ["ag","ah","ai","bg","bh","bi","cg","ch","ci"]
The problem statement explains that we have to generate all the possible combinations of characters that can be obtained from the respective digit of the phone’s keypad. Looking over the given example,
But, How will we do that?
You can try to think of a recursive approach as it is very intuitive, where at each stage of recursion there will be a digit that will offer the respective set of characters, and then we will recursively send the next digit to look for those set of characters that could be appended in our result string.
Look at the below recursion tree for the above example.

The green circles show the base case for the recursion when the length of the string is equal to the length of input digits.
What if we have more than 2 digits as input?
In that case, the recursion tree will have further children until the result string length will be equal to the number of input digit(Why?).
In short, We keep adding each possible letter recursively and this will generate all the possible strings.
Is there an iterative solution to this problem?
Generally, we can convert all-recursive solution to an iterative solution. Here, if we look at the recursion tree, you can draw conclusions that to get all possible strings, we can make a DFS search or a BFS search. If we use the DFS approach then we can use stack and if we use BFS then we can use a queue. However, the answer would be the same in both the iterative approach.
If we will use the BFS approach, then we can take a queue of strings and will push an empty string to it. For all the corresponding keypad characters of the input digit, we will be appending those characters to the front of the queue and then pushing them into the queue until the length of strings in the queue becomes equal to the length of the input digit string.
You can practice this problem here.
Solution Steps
recurse(combination, next_digits)letters mapping the next available digit.letter to the current combination combination = combination + letter.recurse(combination + letter, next_digits[1:]).Pseudo Code
Backtracking / Recursive Approach
string[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }
// driver function
string[] letterCombinations(String digits) {
string[] res
combination("", digits, 0, res)
return res
}
// recursive function
void combination(string prefix, String digits, int idx, string[] res) {
if (idx >= digits.length()) {
res.add(prefix)
return
}
string letters = KEYS[int(digits[idx])]
for (int i = 0 to i < letters.length()) {
combination(prefix + letters[i], digits, idx + 1, res)
}
}
Iterative Approach (BFS)
// bfs utility function
string[] combination(string digits, int n, string[] KEYS) {
string[] list
Queue q
q.push("")
while (q is not empty) {
string s = q.front()
q.pop()
// If complete word is generated push it in the list
if (s.length() == n)
list.add(s)
else
for (letter in KEYS[int(digits[s.length()])])
q.push(s + letter)
}
return list
}
// driver function
string[] letterCombinations(string digits) {
string KEYS[10] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }
int n = digits.length()
string[] res = combination(digits, n, KEYS)
return res
}
Complexity Analysis
where N is the number of digits in the input that maps to 3 letters and M is the number of digits in the input that maps to 4 letters, N+M is the total number digits in the input.
Critical Ideas To Think
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
AfterAcademy Tech
Given two integers n and k, Write a program to return all possible combinations of k numbers out of 1 2 3 ... n. Elements in a combination must be in a non-descending order. The combinations themselves must be sorted in ascending order, i.e., the combination with the smallest first element should be printed first.

AfterAcademy Tech
Given an array of integers arr[] and a target number k, write a program to find all unique combinations in arr[] such that the sum of all integers in the combination is equal to k. This famous backtracking problem has previously been asked in Adobe, Amazon, Facebook.

AfterAcademy Tech
Given an array arr[] consists of 0, 1, 2, 3, 4,.....n. But one of the numbers is missing in it. Write a program to find the number that is missing from the array.

AfterAcademy Tech
Given an array of non-negative integers arr[] of length n, where each element represents the max number of steps that can be made forward from that element. You are initially positioned at the first index of the array. Write a program to return the minimum number of jumps to reach the last index of the array.
