-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoneKeypad.cpp
More file actions
53 lines (40 loc) · 1.01 KB
/
phoneKeypad.cpp
File metadata and controls
53 lines (40 loc) · 1.01 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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void solve(string digits, int index, string output, vector<string> &ans, string mapping[])
{
// base case
if (index >= digits.length())
{
ans.push_back(output);
return;
}
int currDig = digits[index] - '0'; // = 2
string val = mapping[currDig]; // = abc
for (int i = 0; i < val.length(); i++)
{
output.push_back(val[i]);
solve(digits, index + 1, output, ans, mapping);
output.pop_back();
}
}
vector<string> phoneKeypad(string digits)
{
vector<string> ans;
if (digits.length() == 0)
return ans;
string output = "";
int index = 0;
string mapping[10] = {"", "", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz"};
solve(digits, index, output, ans, mapping);
return ans;
}
int main()
{
vector<string> ans;
string input = "12";
ans = phoneKeypad(input);
return 0;
}