forked from sunstick/code-street
-
Notifications
You must be signed in to change notification settings - Fork 0
/
letter_combination.cpp
40 lines (32 loc) · 1023 Bytes
/
letter_combination.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
/*
Given a digit string, return all possible letter combinations that the number could represent.
*/
class Solution {
public:
void solve(int off, string &digits, string candidate, vector<string> &res, string mapping[]) {
if (off == digits.size()) {
res.push_back(candidate);
return;
}
int c = digits[off] - '0';
string x = mapping[c];
for (int i = 0; i < x.size(); i++)
solve(off + 1, digits, candidate + x[i], res, mapping);
}
vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> res;
string mapping[10];
mapping[2] = "abc";
mapping[3] = "def";
mapping[4] = "ghi";
mapping[5] = "jkl";
mapping[6] = "mno";
mapping[7] = "pqrs";
mapping[8] = "tuv";
mapping[9] = "wxyz";
solve(0, digits, "", res, mapping);
return res;
}
};