-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterPhone.cpp
More file actions
56 lines (50 loc) · 1.1 KB
/
LetterPhone.cpp
File metadata and controls
56 lines (50 loc) · 1.1 KB
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
/*Question:
Letter Phone
Asked in:
Facebook
Epic systems
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
The digit 0 maps to 0 itself.
The digit 1 maps to 1 itself.
Input: Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Make sure the returned strings are lexicographically sorted.*/
map<char ,string> m;
void foo()
{
m['0']="0";
m['1']="1";
m['2']="abc";
m['3']="def";
m['4']="ghi";
m['5']="jkl";
m['6']="mno";
m['7']="pqrs";
m['8']="tuv";
m['9']="wxyz";
}
vector<string> ret;
void foo(string A,int i,string ans)
{
if(i>=A.size())
{
if(ans!="")
ret.push_back(ans);
return ;
}
string temp=m[A[i]];
//cout<<temp<<endl;
for(int j=0;j<temp.size();j++)
{
foo(A,i+1,ans+temp[j]);
}
}
vector<string> Solution::letterCombinations(string A) {
foo();
ret.clear();
string ans="";
foo(A,0,ans);
sort(ret.begin(),ret.end());
return ret;
}