Phone Number Combinations - Amazon Top Interview Questions
Problem Statement :
Given a string digits containing 2 to 9 inclusive, return in sorted lexicographic order all possible strings it could represent when mapping to letters on a phone dialpad. These are the mappings on a phone dialpad: | 2 | abc | | 3 | def | | 4 | ghi | | 5 | jkl | | 6 | mno | | 7 | pqrs | | 8 | tuv | | 9 | wxyz | Example 1 Input digits = "23" Output ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
Solution :
Solution in C++ :
vector<string> keypad = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> solve(string digits) {
if (digits.empty()) return {""};
vector<string> answers;
for (auto first : keypad[digits[0] - '0']) {
for (auto rest : solve(digits.substr(1))) {
answers.push_back(first + rest);
}
}
return answers;
}
Solution in Java :
import java.util.*;
class Solution {
public String[] solve(String digits) {
Map<Character, char[]> digitToChars = new HashMap<>();
digitToChars.put('2', new char[] {'a', 'b', 'c'});
digitToChars.put('3', new char[] {'d', 'e', 'f'});
digitToChars.put('4', new char[] {'g', 'h', 'i'});
digitToChars.put('5', new char[] {'j', 'k', 'l'});
digitToChars.put('6', new char[] {'m', 'n', 'o'});
digitToChars.put('7', new char[] {'p', 'q', 'r', 's'});
digitToChars.put('8', new char[] {'t', 'u', 'v'});
digitToChars.put('9', new char[] {'w', 'x', 'y', 'z'});
List<String> collector = new ArrayList<>();
backtrack(digits, 0, digitToChars, new StringBuilder(), collector);
return collector.toArray(new String[collector.size()]);
}
private void backtrack(String digits, int idx, Map<Character, char[]> digitToChars,
StringBuilder sb, List<String> collector) {
if (digits.length() == idx) {
collector.add(sb.toString());
} else {
for (char c : digitToChars.get(digits.charAt(idx))) {
sb.append(c);
backtrack(digits, idx + 1, digitToChars, sb, collector);
sb.deleteCharAt(sb.length() - 1);
}
}
}
}
Solution in Python :
class Solution:
def solve(self, digits):
phone = {}
phone["2"] = list("abc")
phone["3"] = list("def")
phone["4"] = list("ghi")
phone["5"] = list("jkl")
phone["6"] = list("mno")
phone["7"] = list("pqrs")
phone["8"] = list("tuv")
phone["9"] = list("wxyz")
result = []
curr = []
def build(d, curr):
nonlocal phone, result
if len(d) == 0:
result.append("".join(curr))
return
for i in phone[d[0]]:
curr.append(i)
build(d[1:], curr)
curr.pop()
build(list(digits), [])
return result
View More Similar Problems
Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →Binary Search Tree : Insertion
You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <
View Solution →