-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0017.cpp
More file actions
30 lines (23 loc) · 680 Bytes
/
0017.cpp
File metadata and controls
30 lines (23 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
unordered_map<char, string> ht{{'2', "abc"}, {'3', "def"}, {'4', "ghi"},
{'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"},
{'8', "tuv"}, {'9', "wxyz"}};
void backtrack(int i, string dig, string comb, vector<string> &res) {
if (i == dig.size()) {
res.push_back(comb);
return;
}
string let = ht[dig[i]];
for (char c : let) {
backtrack(i + 1, dig, comb + c, res);
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if (digits.size() == 0)
return res;
backtrack(0, digits, "", res);
return res;
}
};