-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.cpp
38 lines (35 loc) · 964 Bytes
/
17.cpp
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
31
32
33
34
35
36
37
38
class Solution {
public:
unordered_map <int, string> map = {
{0, " "},
{1, ""},
{2, "abc"},
{3, "def"},
{4, "ghi"},
{5, "jkl"},
{6, "mno"},
{7, "pqrs"},
{8, "tuv"},
{9, "wxyz"}
};
vector <string> letterCombinations(string digits) {
vector <string> result;
string buffer;
convert(result, digits, 0, buffer);
return result;
}
void convert(vector <string> &result, string digits, int curr, string buffer) {
if (curr == digits.size() && buffer != "") {
result.push_back(buffer);
return;
}
else {
string dict = map[digits[curr] - '0'];
for (int i = 0; i < dict.size(); ++i) {
buffer.push_back(dict[i]);
convert(result, digits, curr + 1, buffer);
buffer.pop_back();
}
}
}
};