-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.cpp
71 lines (70 loc) · 2.15 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <map>
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
std::map<int, vector<string>> m;
vector<string> letterCombinations(string digits) {
vector<string> v2 = {"a","b","c"};
m[2] = v2;
vector<string> v3 = {"d","e","f"};
m[3] = v3;
vector<string> v4 = {"g","h","i"};
m[4] = v4;
vector<string> v5 = {"j","k","l"};
m[5] = v5;
vector<string> v6 = {"m","n","o"};
m[6] = v6;
vector<string> v7 = {"p","q","r","s"};
m[7] = v7;
vector<string> v8 = {"t","u","v"};
m[8] = v8;
vector<string> v9 = {"w","x","y","z"};
m[9] = v9;
vector<string> res{};
for(auto i=0;i<digits.length();++i) {
auto cur_val = std::stoi(digits.substr(i,1));
auto cur_letters = m[cur_val];
auto prev_res_length = res.size();
for (auto j=1;j<cur_letters.size();++j) {
for (auto k=0;k<prev_res_length;++k) {
res.push_back(res[k]);
}
}
/*
cout << "res: [";
for(int j=0;j<res.size();++j) {
cout << res[j] << " ";
}
cout << "] \n";
*/
if(i == 0) {
res.insert(res.end(),cur_letters.begin(),cur_letters.end());
//res.extend(cur_letters);
/*
cout << "res after extend: [";
for(int j=0;j<res.size();++j) {
cout << res[j] << " ";
}
cout << "] \n";
*/
}
else {
for (auto j=0;j<cur_letters.size();++j) {
for (auto k=0;k<prev_res_length;++k) {
res[k+j*prev_res_length].append(cur_letters[j]);
}
}
/*
cout << "res after extend: [";
for(int j=0;j<res.size();++j) {
cout << res[j] << " ";
}
cout << "] \n";
*/
}
}
return res;
}
};