-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path17.cpp
More file actions
29 lines (29 loc) · 896 Bytes
/
17.cpp
File metadata and controls
29 lines (29 loc) · 896 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
29
class Solution {
public:
vector<string> res;
void backtracking(unordered_map<char, string>& mp, string& digits, int currIdx, string& currPath) {
if (currPath.size() == digits.size()) res.push_back(currPath);
else {
for (char c : mp[digits[currIdx]]) {
currPath.push_back(c);
backtracking(mp, digits, currIdx + 1, currPath);
currPath.pop_back();
}
}
}
vector<string> letterCombinations(string digits) {
if (digits.size() == 0) return {};
unordered_map<char, string> mp;
mp['2'] = "abc";
mp['3'] = "def";
mp['4'] = "ghi";
mp['5'] = "jkl";
mp['6'] = "mno";
mp['7'] = "pqrs";
mp['8'] = "tuv";
mp['9'] = "wxyz";
string path = "";
backtracking(mp, digits, 0, path);
return res;
}
};