17. Letter Combinations of a Phone Number
An example of a recursive algorithm

Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
Search for a command to run...
An example of a recursive algorithm

Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
No comments yet. Be the first to comment.
A simple example of using C++ switch

Two dynamic programming techniques to solve Leetcode 120. Triangle. One has space complexity O(n^2). The other is O(n).

An example of using a sliding window approach and an unordered map to track character positions

A simple C++ solution to Leetcode 1695. Maximum Erasure Value using a sliding window approach and prefix sums.

Strategies to avoid them will help you excel in your following technical interview

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Input: digits = ""
Output: []
Input: digits = "2"
Output: ["a","b","c"]
0 <= digits.length <= 4.
digits[i] is a digit in the range ['2', '9'].
If you know the combinations result of a string digits, what is the result of extending it one more digit?
Answer: The new result is constructed by adding each letter of the mapping of the new digit to each string of the result.
Assume you have computed the result of digits = "2", which is ["a","b","c"].
To compute the result of digits = "23", you add each letter of the mapping '3' -> {'d', 'e', 'f'} to each string "a", "b", "c".
You get the new result ["ad","ae","af","bd","be","bf","cd","ce","cf"].
#include <vector>
#include <iostream>
#include <unordered_map>
using namespace std;
void combination(string& digits, unordered_map<char, vector<char> >& m,
int i, vector<string>& result) {
if (i >= digits.length()) {
return;
}
if (result.empty()) {
result = {""};
}
vector<string> newResult;
for (string& s : result) {
for (auto c : m[digits[i]]) {
newResult.push_back(s + c);
}
}
result.swap(newResult);
combination(digits, m, i + 1, result);
}
vector<string> letterCombinations(string digits) {
unordered_map<char, vector<char> > m{{'2', {'a', 'b', 'c'}},
{'3', {'d', 'e', 'f'}},
{'4', {'g', 'h', 'i'}},
{'5', {'j', 'k', 'l'}},
{'6', {'m', 'n', 'o'}},
{'7', {'p', 'q', 'r', 's'}},
{'8', {'t', 'u', 'v'}},
{'9', {'w', 'x', 'y', 'z'}}};
vector<string> result;
combination(digits, m, 0, result);
return result;
}
void printResult(vector<string>& result) {
cout << "[";
for (string& s : result) {
cout << s << ",";
}
cout << "]\n";
}
int main() {
vector<string> result = letterCombinations("23");
printResult(result);
result = letterCombinations("");
printResult(result);
result = letterCombinations("2");
printResult(result);
}
Output:
[ad,ae,af,bd,be,bf,cd,ce,cf,]
[]
[a,b,c,]
Runtime: O(3^N), where N = digits.length. In this problem, N is very small (N <= 4).
Extra space: O(1) (the small map).
You can use the assignment operator '=' for result.swap(newResult), i.e. result = newResult.
But this assignment allocates additional memory for a copy of newResult before assigning it to result.
The std::swap() algorithm avoids such copying by using std::move(). It exchanges the contents of each other without allocating additional memory.
Thanks for reading. Feel free to share your thought about my content and check out my FREE book “10 Classic Coding Challenges”.