human
Count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space | A Dynamic programming based function to count decodings in digit sequence.
For base condition "01123" should return 0.
Using last two calculated values , calculate for ith index.
Change boolean to int.
Return the required answer.
Driver Code.
Function call"
How can the above be solved in Python?
assistant
#Here is code to count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space and the other steps required.
 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 ) ) ;