- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.cpp
More file actions
Latest commit
45 lines (40 loc) · 1.03 KB
/
Copy pathdecode.cpp
File metadata and controls
45 lines (40 loc) · 1.03 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
#include<iostream>
#include<cstring>
usingnamespacestd;
// A Dynamic Programming based function to count decodings
intcountDecodingDP(char *digits, int n)
{
int count[101]; // A table to store results of subproble
count[0] = 1;
if (digits[0] >='1' && digits[0] <= '9')
count[1] = 1;
elsereturn0;
for (int i = 2; i <= n; i++)
{
if (digits[i - 1] >='1' && digits[i - 1] <= '9')
{
if ((digits[i-2] == '1' && digits[i-1] >='0' && digits[i-1] <= '9')
|| digits[i-2] == '2' && digits[i-1] >='0' && digits[i-1] <= '6')
count[i] = count[i -1] + count[i-2];
else
count[i] = count[i-1];
}
else
{
if ((digits[i-2] == '1' && digits[i-1] >='0' && digits[i-1] <= '9')
|| digits[i-2] == '2' && digits[i-1] >='0' && digits[i-1] <= '6')
count[i] = count[i-2];
else
return0;
}
}
return count[n];
}
// Driver program to test above function
intmain()
{
char digits[] = "109";
int n = strlen(digits);
cout << "Count is " << countDecodingDP(digits, n);
return0;
}