-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0017.cpp
More file actions
28 lines (26 loc) · 893 Bytes
/
0017.cpp
File metadata and controls
28 lines (26 loc) · 893 Bytes
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
class Solution {
private:
map<char,string> m;
void pushCombinations(vector<string> &combinations, string &digits, int index, string current) {
if (current.length() == digits.length()) {
combinations.push_back(current);
return;
}
string mappedString = m[digits[index]];
for (char c : mappedString) {
pushCombinations(combinations, digits, index + 1, current + c);
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> combinations;
if (digits.length() > 0)
pushCombinations(combinations, digits, 0, "");
return combinations;
}
Solution() {
m['1'] = ""; m['2'] = "abc"; m['3'] = "def";
m['4'] = "ghi"; m['5'] = "jkl"; m['6'] = "mno";
m['7'] = "pqrs"; m['8'] = "tuv"; m['9'] = "wxyz";
}
};