-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from Coder-2022/main
Create Print_Keypad_Combinations_Code.cpp
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* Given an integer n, using phone keypad find out and print all the possible strings that can be made using digits of input n. | ||
Note : The order of strings are not important. Just print different strings in new lines. | ||
Input Format : | ||
Integer n | ||
Output Format : | ||
All possible strings in different lines | ||
Constraints : | ||
1 <= n <= 10^6 | ||
Sample Input: | ||
23 | ||
Sample Output: | ||
ad | ||
ae | ||
af | ||
bd | ||
be | ||
bf | ||
cd | ||
ce | ||
cf */ | ||
|
||
|
||
|
||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
void printer(string* temp, int n, string output) | ||
{ | ||
if (n==0) | ||
{ | ||
cout << output << endl; | ||
return; | ||
} | ||
int last_digit = n % 10; | ||
string s = temp[last_digit]; | ||
|
||
for(int i=0; i<s.size(); i++) | ||
{ | ||
printer(temp, n / 10, s[i]+output); | ||
} | ||
if(s.size()==0) | ||
{ | ||
printer(temp, n / 10, output); | ||
} | ||
} | ||
void printKeypad(int n) | ||
{ | ||
string* temp = new string[10]; | ||
temp[0] = ""; | ||
temp[1] = ""; | ||
temp[2] = "abc"; | ||
temp[3] = "def"; | ||
temp[4] = "ghi"; | ||
temp[5] = "jkl"; | ||
temp[6] = "mno"; | ||
temp[7] = "pqrs"; | ||
temp[8] = "tuv"; | ||
temp[9] = "wxyz"; | ||
printer(temp, n, ""); | ||
} | ||
|
||
int main(){ | ||
int num; | ||
cin >> num; | ||
|
||
printKeypad(num); | ||
|
||
return 0; | ||
} |