This problem looks busy at first, but the accepted solution is built around one steady invariant. For 17. Letter Combinations of a Phone Number, the solution in this repository is mainly a backtracking solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: backtracking.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are letterCombinations, digit2letters, backtrack.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(3^N * 4^M), space: O(3^N * 4^M)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Letter Combinations of a Phone Number.
02//Memory Usage: 6.8 MB, less than 34.49% of C++ online submissions for Letter Combinations of a Phone Number.
03class Solution {
04public:
05 vector<string> letterCombinations(string digits) {
06 if(digits.empty()) return vector<string>();
07
08 vector<string> digit2letters(10);
09
10 digit2letters[2] = "abc";
11 digit2letters[3] = "def";
12 digit2letters[4] = "ghi";
13 digit2letters[5] = "jkl";
14 digit2letters[6] = "mno";
15 digit2letters[7] = "pqrs";
16 digit2letters[8] = "tuv";
17 digit2letters[9] = "wxyz";
18
19 vector<string> ans = {""};
20
21 for(const char& c : digits){
22 int d = c-'0';
23 vector<string> olds = ans;
24 ans.clear();
25 for(const string& old : olds){
26 for(const char& c : digit2letters[d]){
27 ans.push_back(old+c);
28 }
29 }
30 }
31
32 return ans;
33 }
34};
35
36//backtracking
37//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Letter Combinations of a Phone Number.
38//Memory Usage: 6.8 MB, less than 36.51% of C++ online submissions for Letter Combinations of a Phone Number.
39//N: number of digits in "digits" mapping to 3 chars, M: number of digits in "digits" mapping to 4 chars
40//time: O(3^N * 4^M), space: O(3^N * 4^M)
41class Solution {
42public:
43 vector<string> digit2letters;
44 vector<string> ans;
45
46 void backtrack(string& comb, string next_digits){
47 if(next_digits.empty()){
48 ans.push_back(comb);
49 }else{
50 for(const char& c : digit2letters[next_digits[0]-'0']){
51 comb += c;
52 backtrack(comb, next_digits.substr(1));
53 comb.pop_back();
54 }
55 }
56 }
57
58 vector<string> letterCombinations(string digits) {
59 digit2letters = vector<string>(10);
60 digit2letters[2] = "abc";
61 digit2letters[3] = "def";
62 digit2letters[4] = "ghi";
63 digit2letters[5] = "jkl";
64 digit2letters[6] = "mno";
65 digit2letters[7] = "pqrs";
66 digit2letters[8] = "tuv";
67 digit2letters[9] = "wxyz";
68
69 if(digits.empty()) return vector<string>();
70
71 string comb = "";
72 backtrack(comb, digits);
73
74 return ans;
75 }
76};
Cost