-
Notifications
You must be signed in to change notification settings - Fork 481
/
0017.cpp
38 lines (36 loc) · 1.02 KB
/
0017.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<string> letterCombinations(string digits)
{
vector<string> result;
if (digits.empty()) return result;
findCombination(digits, 0, string(""), result);
return result;
}
private:
const vector<string> letterMap = {
" ","","abc",
"def","ghi","jkl",
"mno","pqrs","tuv","wxyz"};
void findCombination(const string& digits, int index, const string& s, vector<string>& res)
{
if (digits.length() == index)
{
res.push_back(s);
return;
}
char ch = digits[index];
string letters = letterMap[ch - '0'];
for (auto& letter : letters)
{
findCombination(digits, index + 1, s + letter, res);
}
}
};