Skip to content

Commit 5e033e9

Browse files
authored
Keypad Problem using Recursion
1 parent 12463ec commit 5e033e9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

keypadproblem.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
using namespace std;
4+
string keypad[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
5+
void keyp(string s, string ans)
6+
{
7+
if (s.size() == 0)
8+
{
9+
cout << ans << endl;
10+
return;
11+
}
12+
char ch = s[0];
13+
string code = keypad[ch - '0'];
14+
string ros = s.substr(1);
15+
for (int i = 0; i < code.length(); i++)
16+
{
17+
keyp(ros, ans + code[i]);
18+
}
19+
}
20+
int main(int argc, char const *argv[])
21+
{
22+
keyp("23", "");
23+
cout << endl;
24+
return 0;
25+
}

0 commit comments

Comments
 (0)