traverse in reverse order
for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( len ( v [ s [ i ] ] ) ) : NEW_LINE INDENT dp [ s [ i ] ] += dp [ v [ s [ i ] ] [ j ] ] NEW_LINE DEDENT DEDENT return dp NEW_LINE