-
Notifications
You must be signed in to change notification settings - Fork 0
/
countTranslation.cpp
49 lines (40 loc) · 1.05 KB
/
countTranslation.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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int CountTranslation(const string& numStr) {
int length = numStr.length();
vector<int> counts(length);
int count = 0;
for (int i = length - 1; i >= 0; i--) {
count = 0;
if (i < length - 1)
count = counts[i+1];
else
count = 1;
if (i < length - 1) {
int number1 = numStr[i] - '0';
int number2 = numStr[i+1] - '0';
int converted = number1 * 10 + number2;
if (converted >= 10 && converted <= 25) {
if (i < length - 2)
count += counts[i+2];
else
count += 1;
}
}
counts[i] = count;
}
return counts[0];
}
int CountTranslation(int number) {
if (number < 0)
return 0;
string numStr = to_string(number);
return CountTranslation(numStr);
}
int main() {
int number = 1234215;
cout << CountTranslation(number) << endl;
return 0;
}