-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCombinationsOfAPhoneNumber.cc
More file actions
55 lines (43 loc) · 1.92 KB
/
LetterCombinationsOfAPhoneNumber.cc
File metadata and controls
55 lines (43 loc) · 1.92 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
54
55
namespace LetterCombinationsOfAPhoneNumber {
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
unordered_map<char, vector<char>> m;
m['0'] = vector<char>();
m['1'] = vector<char>();
char charsFor2[] = {'a', 'b', 'c'};
m['2'] = vector<char>(charsFor2, charsFor2 + 3);
char charsFor3[] = {'d', 'e', 'f'};
m['3'] = vector<char>(charsFor3, charsFor3+ 3);
char charsFor4[] = {'g', 'h', 'i'};
m['4'] = vector<char>(charsFor4, charsFor4 + 3);
char charsFor5[] = {'j', 'k', 'l'};
m['5'] = vector<char>(charsFor5, charsFor5 + 3);
char charsFor6[] = {'m', 'n', 'o'};
m['6'] = vector<char>(charsFor6, charsFor6 + 3);
char charsFor7[] = {'p', 'q', 'r', 's'};
m['7'] = vector<char>(charsFor7, charsFor7 + 4);
char charsFor8[] = {'t', 'u', 'v'};
m['8'] = vector<char>(charsFor8, charsFor8 + 3);
char charsFor9[] = {'w', 'x', 'y', 'z'};
m['9'] = vector<char>(charsFor9, charsFor9 + 4);
res.push_back("");
for (int i = 0; i < digits.size(); i++) {
char d = digits[i];
vector<char> chars = m[d];
int n = res.size();
for (int j = 0; j < n; j++) {
string s = res.front();
for (int k = 0; k < chars.size(); k++) {
s.push_back(chars[k]);
res.push_back(s);
s.pop_back();
}
res.erase(res.begin()); // pop front
}
}
return res;
}
};
}