-
Notifications
You must be signed in to change notification settings - Fork 0
/
letterCombinations.txt
29 lines (26 loc) · 1.04 KB
/
letterCombinations.txt
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
17. Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
class Solution {
public:
void DFS(string digits, int pos, string &path, vector<string> &res, vector<string> &letter){
if(pos == digits.length()){
res.push_back(path);
return;
}
for(char c: letter[digits[pos] - '0']){
path.push_back(c);
DFS(digits,pos+1,path,res,letter);
path.pop_back();
}
}
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;
}
};