-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone.cpp
53 lines (48 loc) · 1.27 KB
/
phone.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
//
// phone.cpp
// xcode
//
// Created by Gokul Nadathur on 8/21/16.
// Copyright © 2016 Gokul Nadathur. All rights reserved.
//
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
class Solution {
public:
vector<string> letterCombinations(string digits) {
unordered_map<char, string> ntos = {{ '2', string("abc")},
{ '3', string("def")},
{ '4', string("ghi")},
{ '5', string("jkl")},
{ '6', string("mno")},
{ '7', string("pqrs")},
{ '8', string("tuv")},
{ '9', string("wxyz")}};
vector<string> res;
for (auto it = digits.rbegin(); it != digits.rend(); ++it) {
char c = *it;
auto strs = ntos[c];
vector<string> tmp;
for (auto chr : strs) {
if (it == digits.rbegin()) {
tmp.push_back(string(1,chr));
} else {
for (auto& s : res) {
tmp.push_back(string(1, chr) + s);
}
}
}
res = tmp;
}
return res;
}
};
int main(int argc, char** argv)
{
string inp("2");
Solution s;
s.letterCombinations(inp);
return 0;
}