Skip to content

Commit d0d5598

Browse files
committed
Again i think it is backtracking, yeah,cool
1 parent c14dd25 commit d0d5598

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
vector<string> output;
4+
void backtrack(int start,map<char,string> phone,string temp,string digits)
5+
{
6+
if(start>=digits.length())
7+
{
8+
output.push_back(temp);
9+
return;
10+
}
11+
for(auto x:phone[digits[start]])
12+
{
13+
temp.push_back(x);
14+
backtrack(start+1,phone,temp,digits);
15+
temp.pop_back();
16+
17+
}
18+
}
19+
vector<string> letterCombinations(string digits)
20+
{
21+
if(digits=="")
22+
{
23+
return output;
24+
}
25+
map<char,string> phone;
26+
phone['2'] = "abc";
27+
phone['3'] = "def";
28+
phone['4'] = "ghi";
29+
phone['5'] = "jkl";
30+
phone['6'] = "mno";
31+
phone['7'] = "pqrs";
32+
phone['8'] = "tuv";
33+
phone['9'] = "wxyz";
34+
string temp = "";
35+
backtrack(0,phone,temp,digits);
36+
return output;
37+
}
38+
};

0 commit comments

Comments
 (0)