forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter-combinations-of-a-phone-number.cpp
40 lines (39 loc) · 1.13 KB
/
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
class Solution {
public:
unordered_map<char,string> intToCharsMap;
void backtracking(string::iterator lf,string::iterator rt,string &path,vector<string> &result)
{
if(lf == rt)
{
result.push_back(path);
return;
}
for(auto c : intToCharsMap[*lf])
{
path.push_back(c);
backtracking(next(lf,1),rt,path,result);
path.pop_back(); // if a character doesnot matches then we pop that character from the string and again backtrack.
}
}
vector<string> letterCombinations(string digits)
{
int n = digits.size();
if(digits == "")
return {};
string path;
// result array stores every string that represents that digit.
vector<string> result;
intToCharsMap = {
{'2', "abc"},
{'3', "def"},
{'4', "ghi"},
{'5', "jkl"},
{'6', "mno"},
{'7', "pqrs"},
{'8', "tuv"},
{'9', "wxyz"},
};
backtracking(digits.begin(),digits.end(),path,result);
return result;
}
};