-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLetter Combinations of a Phone Number.cpp
More file actions
78 lines (72 loc) · 1.56 KB
/
Letter Combinations of a Phone Number.cpp
File metadata and controls
78 lines (72 loc) · 1.56 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Solution {
public:
vector<vector<char>> phonepad;
vector<string> ans;
void getStrings(int n,string&temp)
{
if(n<=0)
{ ans.push_back(temp);
cout<<temp<<"\n";
if(temp.length()>=1)
temp.erase(temp.length()-1);
return;
}
else
{
int x=1;
int t=n,ct=0;
while(t>0)
{
ct+=1;
t=t/10;
x=x*10;
}
x=x/10;
int left_digit=n/x;
for(int i=0;i<phonepad[left_digit-2].size();i++)
{
temp.insert(temp.length(),1,phonepad[left_digit-2][i]);
getStrings(n%x,temp);
}
if(temp.length()>=1)
temp.erase(temp.length()-1);
}
}
vector<string> letterCombinations(string digits) {
int temp=97;
for(int i=0;i<8;i++)
{
vector<char> v;
if(i!=7 && i!=5)
{
for(int j=0;j<3;j++)
{
v.push_back((char)temp);
temp+=1;
}
}
else
{
for(int j=0;j<=3;j++)
{
v.push_back((char)temp);
temp+=1;
}
}
phonepad.push_back(v);
}
bool found=false;
for(int i=0;i<digits.length();i++)
{
if(digits[i]=='1')
{
found=true;
break;
}
}
string k="";
if(!found && digits.length()>0)
getStrings(stoi(digits),k);
return ans;
}
};