-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path017. Letter Combinations of a Phone Number.cpp
56 lines (53 loc) · 1.84 KB
/
017. Letter Combinations of a Phone Number.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
54
55
56
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits.size() == 0) return res;
string s = "";
unordered_map<char,vector<int>>m({{'2', {'a','b','c'}},
{'3', {'d','e','f'}},
{'4', {'g','h','i'}},
{'5', {'j','k','l'}},
{'6', {'m','n','o'}},
{'7', {'p','q','r','s'}},
{'8', {'t','u','v'}},
{'9', {'w','x','y','z'}}});
DFS(m, digits, 0, res, s);
return res;
}
void DFS(unordered_map<char,vector<int>>& m, string& digits, int l, vector<string>& res, string& s){
if(l == digits.size()){
res.push_back(s);
return;
}
for(int i = 0; i < m[digits[l]].size(); i++){
s.push_back(m[digits[l]][i]);
DFS(m, digits, l+1, res, s);
s.pop_back();
}
return;
}
};
// Update
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string>res;
if(digits.empty()) return res;
vector<string>letter({"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"});
string path = "";
DFS(digits, 0, path, res, letter);
return res;
}
void DFS(string digits, int pos, string& path, vector<string>& res, vector<string>& letter){
if(pos == digits.size()){
res.push_back(path);
return;
}
for(auto c: letter[digits[pos] - '0']){
path.push_back(c);
DFS(digits, pos + 1, path, res, letter);
path.pop_back();
}
}
};