-
Notifications
You must be signed in to change notification settings - Fork 255
/
Mapped strings.cpp
59 lines (52 loc) · 1.11 KB
/
Mapped strings.cpp
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
/*We are given a hashmap which maps all the letters with number. Given 1 is mapped with A, 2 is mapped with B…..26 is mapped with Z. Given a number, you have
to print all the possible strings.
Input Format
A single line contains a number.
Constraints
Number is less than 10^6
Output Format
Print all the possible strings in sorted order in different lines.
Sample Input
123
Sample Output
ABC
AW
LC
Explanation
'1' '2' '3' = ABC
'1' '23' = AW
'12' '3' = LC*/
#include<iostream>
using namespace std;
void generate_strings(char *in,char *out,int i,int j){
//base case
if(in[i]=='\0'){
out[j]='\0';
cout<<out<<endl;
return;
}
//recursive case
//one digit at a time
int digit=in[i]-'0';
char ch=digit+'A'-1;
out[j]=ch;
generate_strings(in,out,i+1,j+1);
//two digit at a time
if(in[i+1]!='\0'){
int seconddigit=in[i+1]-'0';
int no=digit*10+seconddigit;
if(no<=26){
ch=no+'A'-1;
out[j]=ch;
generate_strings(in,out,i+2,j+1);
}
}
return;
}
int main(){
char in[1000000];
cin>>in;
char out[100];
generate_strings(in,out,0,0);
return 0;
}