forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.cpp
37 lines (31 loc) · 991 Bytes
/
sol.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
// A naive recursive C++ implementation to count number of decodings
// that can be formed from a given digit sequence
#include <iostream>
#include <cstring>
using namespace std;
// Given a digit sequence of length n, returns count of possible
// decodings by replacing 1 with A, 2 woth B, ... 26 with Z
int countDecoding(char *digits, int n)
{
// base cases
if (n == 0 || n == 1)
return 1;
int count = 0; // Initialize count
// If the last digit is not 0, then last digit must add to
// the number of words
if (digits[n-1] > '0')
count = countDecoding(digits, n-1);
// If the last two digits form a number smaller than or equal to 26,
// then consider last two digits and recur
if (digits[n-2] == '1' || (digits[n-2] == '2' && digits[n-1] < '7') )
count += countDecoding(digits, n-2);
return count;
}
// Driver program to test above function
int main()
{
char digits[] = "1234";
int n = strlen(digits);
cout << "Count is " << countDecoding(digits, n);
return 0;
}