Skip to content

Adding a new commit #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions challenges/decode ways.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <cstring>
using namespace std;

// A Dynamic Programming based function
// to count decodings
int countDecodingDP(char *digits, int n)
{
// A table to store results of subproblems
int count[n+1];
count[0] = 1;
count[1] = 1;
//for base condition "01123" should return 0
if(digits[0]=='0')
return 0;
for (int i = 2; i <= n; i++)
{
count[i] = 0;

// If the last digit is not 0,
// then last digit must add to the number of words
if (digits[i-1] > '0')
count[i] = count[i-1];

// If second last digit is smaller
// than 2 and last digit is smaller than 7,
// then last two digits form a valid character
if (digits[i-2] == '1' ||
(digits[i-2] == '2' && digits[i-1] < '7') )
count[i] += count[i-2];
}
return count[n];
}

// Driver program to test above function
int main()
{
char digits[] = "1234";
int n = strlen(digits);
cout << "Count is " << countDecodingDP(digits, n);
return 0;
}