Count Possible Decodings of a given Digit Sequence in O(N) time and Constant Auxiliary space
Last Updated :
15 Jun, 2021
Given a digit sequence S, the task is to find the number of possible decodings of the given digit sequence where 1 represents ‘A’, 2 represents ‘B’ … and so on up to 26, where 26 represents ‘Z’.
Examples:
Input: S = “121”
Output: 3
The possible decodings are “ABA”, “AU”, “LA”
Input: S = “1234”
Output: 3
The possible decodings are “ABCD”, “LCD”, “AWD”
Approach: In order to solve this problem in O(N) time complexity, Dynamic Programming is used. And in order to reduce the auxiliary space complexity to O(1), we use the space optimized version of recurrence relation discussed in the Fibonacci Number Post.
Similar to the Fibonacci Numbers, the key observations of any current ‘ith‘ index can be calculated using its previous two indices. So the Recurrence Relation to calculate the ith index can be denoted as
// Condition to check last
// digit can be included or not
if (digit[i-1] is not '0')
count[i] += count[i-1]
// Condition to check the last
// two digits contribution
if (digit[i-2] is 1 or
(digit[i-2] is 2 and
digit[i-1] is less than 7))
count[i] += count[i-2]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDecodingDP(string digits, int n)
{
if (digits[0] == '0' )
return 0;
int count0 = 1, count1 = 1, count2;
for ( int i = 2; i <= n; i++) {
count2 = (( int )(digits[i - 1] != '0' ) * count1) +
( int )((digits[i - 2] == '1' ) or
(digits[i - 2] == '2' and
digits[i - 1] < '7' )) * count0;
count0 = count1;
count1 = count2;
}
return count1;
}
int main()
{
string digits = "1234" ;
int n = digits.size();
cout << countDecodingDP(digits, n);
return 0;
}
|
Java
class GFG{
static int countDecodingDP(String digits, int n)
{
if (digits.charAt( 0 ) == '0' )
{
return 0 ;
}
int count0 = 1 , count1 = 1 , count2;
for ( int i = 2 ; i <= n; i++)
{
int dig1 = 0 , dig2, dig3 = 0 ;
if (digits.charAt(i - 1 ) != '0' )
{
dig1 = 1 ;
}
if (digits.charAt(i - 2 ) == '1' )
{
dig2 = 1 ;
}
else
dig2 = 0 ;
if (digits.charAt(i - 2 ) == '2' &&
digits.charAt(i - 1 ) < '7' )
{
dig3 = 1 ;
}
count2 = dig1 * count1 +
dig2 + dig3 * count0;
count0 = count1;
count1 = count2;
}
return count1;
}
public static void main(String[] args)
{
String digits = "1234" ;
int n = digits.length();
System.out.print(countDecodingDP(digits, n));
}
}
|
Python3
def countDecodingDP(digits, n):
if (digits[ 0 ] = = '0' ):
return 0 ;
count0 = 1 ; count1 = 1 ;
for i in range ( 2 , n + 1 ):
dig1 = 0 ; dig3 = 0 ;
if (digits[i - 1 ] ! = '0' ):
dig1 = 1 ;
if (digits[i - 2 ] = = '1' ):
dig2 = 1 ;
else :
dig2 = 0 ;
if (digits[i - 2 ] = = '2' and
digits[i - 1 ] < '7' ):
dig3 = 1 ;
count2 = dig1 * count1 +
dig2 + dig3 * count0;
count0 = count1;
count1 = count2;
return count1;
if __name__ = = '__main__' :
digits = "1234" ;
n = len (digits);
print (countDecodingDP(digits, n));
|
C#
using System;
class GFG{
static int countDecodingDP(String digits, int n)
{
if (digits[0] == '0' )
{
return 0;
}
int count0 = 1, count1 = 1, count2;
for ( int i = 2; i <= n; i++)
{
int dig1 = 0, dig2, dig3 = 0;
if (digits[i - 1] != '0' )
{
dig1 = 1;
}
if (digits[i - 2] == '1' )
{
dig2 = 1;
}
else
dig2 = 0;
if (digits[i - 2] == '2' &&
digits[i - 1] < '7' )
{
dig3 = 1;
}
count2 = dig1 * count1 +
dig2 + dig3 * count0;
count0 = count1;
count1 = count2;
}
return count1;
}
public static void Main(String[] args)
{
String digits = "1234" ;
int n = digits.Length;
Console.Write(countDecodingDP(digits, n));
}
}
|
Javascript
<script>
function countDecodingDP(digits, n)
{
if (digits[0] == '0' )
return 0;
var count0 = 1, count1 = 1, count2;
for ( var i = 2; i <= n; i++) {
count2 = ((digits[i - 1] != '0' ) * count1) +
((digits[i - 2] == '1' ) ||
(digits[i - 2] == '2' &&
digits[i - 1] < '7' )) * count0;
count0 = count1;
count1 = count2;
}
return count1;
}
var digits = "1234" ;
var n = digits.length;
document.write( countDecodingDP(digits, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Related Article: Count Possible Decodings of a given Digit Sequence
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...