-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathReturnAllCodesString.cpp
More file actions
63 lines (52 loc) · 1.31 KB
/
ReturnAllCodesString.cpp
File metadata and controls
63 lines (52 loc) · 1.31 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
54
55
56
57
58
59
60
61
62
63
// Assume that the value of a = 1, b = 2, c = 3, ... , z = 26. You are given a numeric string S. Write a program to return the list of all possible codes that can be generated from the given string.
// Input Format :
// A numeric string
// Input:
// 1123
// Output:
// aabc
// kbc
// alc
// aaw
// kw
#include <iostream>
using namespace std;
#include <string.h>
using namespace std;
int getCodes(string input, string output[]) {
if(input.length()==0)
{
output[0]="";
return 1;
}
string result1[1000];
int firstDigit=stoi(input.substr(0,1));
char firstCharacter=96+firstDigit;
int x=getCodes(input.substr(1),result1);
for(int i=0; i<x; i++)
{
output[i]=firstCharacter+result1[i];
}
int firstTwoDigits=stoi(input.substr(0,2));
if(firstTwoDigits>=10 && firstTwoDigits<=26)
{
firstCharacter=firstTwoDigits+96;
string result2[1000];
int y=getCodes(input.substr(2),result2);
for(int i=0; i<y; i++)
{
output[i+x]=firstCharacter+result2[i];
}
return x+y;
}
return x;
}
int main(){
string input;
cin >> input;
string output[10000];
int count = getCodes(input, output);
for(int i = 0; i < count && i < 10000; i++)
cout << output[i] << endl;
return 0;
}