Dynamic Programming Series
Dynamic Programming Series
Day 8
Decode Ways..!!
Problem Description
A message containing letters from a to z is being encoded to numbers using the following mapping :
Problem Description
A message containing letters from a to z is being encoded to numbers using the following mapping :
'a' -> 1
'b' -> 2
'c' -> 3
'z' -> 26
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Example :
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Example :
"12"
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Example :
"12"
ab
l
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Example :
"12"
ab
l
Because a = 1 and b = 2 in case 1 and in case 2 we have l = 12
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Example 2:
"226"
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Example 2:
"226"
bz
vf
bff
Problem Description
Task : Find out the numbers of ways of creating different codes.
(given string is non-empty)
Example 2:
"226"
bz
vf
bff
Because bz(2,26) , vf(22,6) and bff(2,2,6)
So now we have understood the problem..!!
So now we have understood the problem..!!
Now the question arises How we can approach to this problem??

What we have learnt till now in this
series ?
Dividing a big problem into smaller subproblems.... that's the first step that
we should take..!!
What we have learnt till now in this
series ?
Dividing a big problem into smaller subproblems.... that's the first step that
we should take..!!
Now how we will do this in this particular problem??
Let us see with some basic cases
If we have a string of length 1
Suppose input is "5"
Then how many ways exist to form the code i.e., only a single way -> 'e'
Let us see with some basic cases
If we have a string of length 1
Suppose input is "5"
Then how many ways exist to form the code i.e., only a single way -> 'e'
This is what we can think at least..!!
If we have string of length 2
Suppose we have a string "15"
Then how many ways exist to form the code
If we have string of length 2
Suppose we have a string "15"
Then how many ways exist to form the code
That is 2 one is "ae" and the other is "o".
If we have string of length 2
Suppose we have a string "15"
Then how many ways exist to form the code
That is 2 one is "ae" and the other is "o".

Simple observations..!!
if we have input as "45"
if we have input as "45"
Then also the answer would be 2.
if we have input as "45"
Then also the answer would be 2.
Hey..!! But wait a minute "45" can exist as "de" but we do not have any character from a...z in 45 we only have 26 characters..!!
That's the game .. to make such important observations
Now following the traditions that we are following so far..!!
1. Developing a recursive approach.
2. Developing an iterative approach.
How recursive thing will work here?
Let us understand through an Example how we form a recursive approach for this question.
Given n = 226
How recursive thing will work here?
Let us understand through an Example how we form a recursive approach for this question.
Given n = 226
226
226
226
b
v
How recursive thing will work here?
Let us understand through an Example how we form a recursive approach for this question.
Given n = 226
226
226
226
b
v
226
bb
226
bz
226
bb
226
bz
How recursive thing will work here?
Let us understand through an Example how we form a recursive approach for this question.
Given n = 226
226
226
226
b
v
226
bb
226
bz
226
bb
226
bz
226
vf
How recursive thing will work here?
Let us understand through an Example how we form a recursive approach for this question.
Given n = 226
226
226
226
b
v
226
bb
226
bz
226
bb
226
bz
226
vf
1
2
3
Let's take one more example to make it real easy.
Take n = 2567
2567
b
y
2567
2567
Let's take one more example to make it real easy.
Take n = 2567
2567
b
y
2567
2567
bb
be
2567
2567
X
Let's take one more example to make it real easy.
Take n = 2567
2567
b
y
2567
2567
bb
be
2567
2567
X
yf
2567
X
2567
Let's take one more example to make it real easy.
Take n = 2567
2567
b
y
2567
2567
bb
be
2567
2567
X
yf
2567
X
2567
bb
bef
2567
X
2567
Let's take one more example to make it real easy.
Take n = 2567
2567
b
y
2567
2567
bb
be
2567
2567
X
yf
2567
X
2567
bb
bef
2567
X
2567
yfg
2567
X
2567
Let's take one more example to make it real easy.
Take n = 2567
2567
b
y
2567
2567
bb
be
2567
2567
X
yf
2567
X
2567
bb
bef
2567
X
2567
yfg
2567
X
2567
1
2
befg
2567
Now we are ready to go for a recursive code for this approach.
int decode(int i, string s)
{
int n = s.size();
if (i == n)
return 1;
if (s[i] == '0')
return 0;
int res = 0;
res = decode(i+1, s);
if (i <= n-2 && (s[i] == '1' || (s[i] == '2' && s[i+1] <= '6')))
res += decode(i+2, s);
return res;
}
int numDecodings(string s) {
if (s.size() == 0)
return 0;
return decode(0, s);
}Let's talk about the time complexity of this Approach.
On the worst case for every number we have two calls.
level 0 = 2^0 = 1
Total levels = n
level 1 = 2^1 = 2
level 2 = 2^2 = 4
Now To reduce the complexity we should be able to find the redundancy in the tree.
Take back our previous example n = 2567
2567
b
y
2567
2567
bb
be
2567
2567
X
yf
2567
X
2567
bb
bef
2567
X
2567
yfg
2567
X
2567
1
2
befg
2567
Now To reduce the complexity we should be able to find the redundancy in the tree.
Can we store the result of of these steps so we don't have to recompute it?
Yes Of course, That's where the DP comes.
Let's talk about the DP approach
Now what we will be doing that we will store the number ways possible till the ith index in our dp array.
Now when we will compute the answer for (i+1)th index we will add the value of dp[i] and dp[i-1](if possible)
We will fill this DP array in Bottom Up Manner.
int n = s.length();
int* dp = new int[n+1]();
if(s[0]-'0'!=0)
dp[0] = 1;
if(s[0]-'0'!=0)
dp[1] = 1;
for(int i = 2; i<=n; i++)
{
if(s[i-1]-'0'!=0)
dp[i] = dp[i-1];
if(((s[i-2]-'0')*10 + (s[i-1] - '0') <= 26) && ((s[i-2]-'0')*10 + (s[i-1] - '0')
>= 10))
dp[i] += dp[i-2];
}
return dp[n];So that's It for Day 8
Concepts that we have learnt
So that's It for Day 8
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. How to handle Edge cases.
So that's It for Day 8
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. How to handle Edge cases..
2. Use of optimal Substructure with memoization
So that's It for Day 8
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. How to handle Edge cases.
2. Use of optimal Substructure with memoization
Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply
OR
Send your doubt to email provided in description.
Copy of Copy of Day 6 vacation problem
By Chirayu Jain
Copy of Copy of Day 6 vacation problem
- 75