This repository was archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreturnAllCodes.cpp
More file actions
80 lines (67 loc) · 1.71 KB
/
Copy pathreturnAllCodes.cpp
File metadata and controls
80 lines (67 loc) · 1.71 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#define debug(a) cout << #a << ':' << a << endl
using namespace std;
/*
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.
Note : The order of codes are not important.
And input string does not contain 0s.
input:
1123
output:
aabc
kbc
alc
aaw
kw
*/
int getCodes(string input, string output[10000])
{
/*
for 1123
1 1 2 3
11 2 3
1 12 3
1 1 23
11 23
*/
if (input.length() == 0)
{
return 1;
}
//pass one less. then add last char equivalent to all outputs.
int last_digit = input[input.length() - 1] - 48; // -48 to convert ascii to int
//debug(last_digit);
char to_add = 'a' + last_digit - 1;
int entries = getCodes(input.substr(0, input.length() - 1), output);
for (int i = 0; i < entries; ++i)
{
output[i].push_back(to_add);
//debug(output[i]);
}
//pass two less. then add last two char equivalent to all outputs.
if (input.length() < 2)
return entries;
int last_num = ((input[input.length() - 2] - 48) * 10) + (input[input.length() - 1] - 48);
if (last_num > 26)
return entries;
int entries2 = getCodes(input.substr(0, input.length() - 2), output + entries);
to_add = 'a' + last_num - 1;
for (int i = 0; i < entries2; ++i)
{
output[i + entries].push_back(to_add);
}
return entries + entries2;
}
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;
}