17. Letter Combinations of a Phone Number
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Backtracking
- Time
- O(4^N)
- Space
- O(N)
Standard Solution (C++)
SummaryGiven a string of digits from 2-9, return all possible letter combinations that the digits could represent on a traditional phone keypad, with each digit mapping to a fixed set of 3-4 letters.
Approach
- Use backtracking (DFS) to build combinations one character at a time, advancing one digit per recursive call.
- At each level, iterate over the letters mapped to the current digit, append the letter to a running path, recurse, then remove it (backtrack).
- When the path length equals the input length, record the complete combination.
- An early-exit guard on empty input avoids pushing an empty string into the result.
#include <vector>
#include <string>
using namespace std;
class Solution {
// Maps digit '2'-'9' to its phone keypad letters
const string kMap[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void backtrack(const string& digits, int idx, string& path, vector<string>& result) {
if (idx == (int)digits.size()) {
result.push_back(path);
return;
}
for (char c : kMap[digits[idx] - '2']) {
path.push_back(c);
backtrack(digits, idx + 1, path, result);
path.pop_back(); // undo choice
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
if (digits.empty()) return result; // guard: no digits -> no combinations
string path;
backtrack(digits, 0, path, result);
return result;
}
}; Time O(4^N * N) — at most 4 letters per digit gives 4^N leaves; copying the N-length path at each leaf costs O(N) Space O(N) auxiliary stack space for recursion depth N (excluding the output vector)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
class Solution {
public:
vector<string> digitsMap={"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> letterCombinations(string digits) {
vector<string> result;
if(digits.length()==0)
return result;
string path="";
DFS(result, path, digits, 0);
return result;
}
void DFS(vector<string>& result, string& path, string& digits, int index) {
if(index==digits.length()){
result.push_back(path);
return;
}
string& letters=digitsMap[digits[index]-'2'];
for(char c: letters){
path.push_back(c);
DFS(result, path, digits, index+1);
path.pop_back();
}
}
};
// Time Complexity: O(4^N), but O(4^N * N) for copying the path to the result.
// Space Complexity: O(N), here we only consider the space of auxiliary space. When considering the space of the result, it is O(4^N * N). Time O(4^N) Space O(N)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.