-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoneKeypadProblem.cpp
More file actions
46 lines (46 loc) · 1.1 KB
/
phoneKeypadProblem.cpp
File metadata and controls
46 lines (46 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void keypadSolve(vector<string> &mapping, int index, vector<string> &ans, string output, string digits)
{
// base Case
if (index >= digits.size())
{
ans.push_back(output);
return;
}
// recursion
int digit = digits[index] - '0';
string value = mapping[digit];
for (int i = 0; i < value.size(); i++)
{ // include character
char ch = value[i];
output.push_back(ch);
//recursion
keypadSolve(mapping, index + 1, ans, output, digits);
// backtracking
output.pop_back();
}
}
int main()
{
string digits = "23";
vector<string> ans;
int index = 0;
string output = "";
vector<string> mapping(10);
mapping[2] = "abc";
mapping[3] = "def";
mapping[4] = "ghi";
mapping[5] = "jkl";
mapping[6] = "mno";
mapping[7] = "pqrs";
mapping[8] = "tuv";
mapping[9] = "wxyz";
keypadSolve(mapping, index, ans, output, digits);
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i] << " ";
}
}