Sum of Digits of the Good Strings | Python3 implementation of the approach ; To store the states of the dp ; Function to fill the dp table ; Sum of digits of the string of length 1 is i as i is only number in that string and count of good strings of length 1 that end with i is also 1 ; Adjacent digits are different ; Increment the count as digit at ( i - 1 ) 'th index is k and count  of good strings is equal to this  because at the end of the strings of  length (i - 1) we are just  putting digit j as the last digit ; Driver code
DIGITS = 10 ; NEW_LINE MAX = 10000 ; NEW_LINE MOD = 1000000007 ; NEW_LINE dp = [ [ 0 for i in range ( DIGITS ) ] for i in range ( MAX ) ] ; NEW_LINE cnt = [ [ 0 for i in range ( DIGITS ) ] for i in range ( MAX ) ] ; NEW_LINE def precompute ( ) : NEW_LINE INDENT for i in range ( DIGITS ) : NEW_LINE INDENT dp [ 1 ] [ i ] = i ; NEW_LINE cnt [ 1 ] [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT for j in range ( DIGITS ) : NEW_LINE INDENT for k in range ( DIGITS ) : NEW_LINE INDENT if ( j != k ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i ] [ j ] + ( dp [ i - 1 ] [ k ] + ( cnt [ i - 1 ] [ k ] * j ) % MOD ) % MOD ; NEW_LINE dp [ i ] [ j ] %= MOD ; NEW_LINE cnt [ i ] [ j ] += cnt [ i - 1 ] [ k ] ; NEW_LINE cnt [ i ] [ j ] %= MOD ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT x = 6 ; y = 4 ; NEW_LINE precompute ( ) ; NEW_LINE print ( dp [ x ] [ y ] ) ; NEW_LINE