Count of N digit Numbers having no pair of equal consecutive Digits | Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update dp table ; Calculate the count of required N - digit numbers ; Driver Code
def count ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( 10 ) ; NEW_LINE return ; NEW_LINE DEDENT dp = [ [ 0 for i in range ( 10 ) ] for j in range ( N ) ] NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for j in range ( 10 ) : NEW_LINE INDENT temp += dp [ i - 1 ] [ j ] ; NEW_LINE DEDENT for j in range ( 10 ) : NEW_LINE INDENT dp [ i ] [ j ] = temp - dp [ i - 1 ] [ j ] ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT ans += dp [ N - 1 ] [ i ] ; NEW_LINE DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE count ( N ) ; NEW_LINE DEDENT