-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathletterCombinations.cpp
More file actions
53 lines (44 loc) · 1.49 KB
/
letterCombinations.cpp
File metadata and controls
53 lines (44 loc) · 1.49 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "lib/misc.h"
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
unordered_map<char, vector<char>> mapping = {{ '1', { '1' } },
{ '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' } },
{ '0', {'0'} },
};
std::reverse(std::begin(digits), std::end(digits));
for (const auto &c : digits) {
vector<string> r = result;
result.clear();
auto letters = mapping[c];
for (char l : letters) {
if (r.empty()) {
string i(1, l);
result.push_back(i);
} else {
for (const auto &str : r) {
result.push_back(l + str);
}
}
}
}
return result;
}
};
int main()
{
Solution s;
auto res = s.letterCombinations("3434323");
for (const auto &x : res) {
printf("%s \n", x.c_str());
}
return 0;
}