human
Can you provide a code to minimum Cost to make two Numeric Strings Identical and the other steps required given the following:
Minimum Cost to make two Numeric Strings Identical | Function to find weight of LCS.
if this state is already calculated then return.
adding required weight for particular match.
recurse for left and right child and store the max.
Function to calculate cost of string.
Driver code.
Minimum cost needed to make two strings identical"
How can the above be solved in Python?
assistant
#Here is code to minimum Cost to make two Numeric Strings Identical and the other steps required.
 def lcs ( dp , a , b , m , n ) :
	for i in range ( 100 ) :
		for j in range ( 100 ) :
			dp [ i ] [ j ] = - 1
	if ( m < 0 or n < 0 ) :
		return 0
	if ( dp [ m ] [ n ] != - 1 ) :
		return dp [ m ] [ n ]
	ans = 0
	if ( a [ m ] == b [ n ] ) :
		ans = ( ord ( a [ m ] ) - 48 ) + lcs ( dp , a , b , m - 1 , n - 1 )
	else :
		ans = max ( lcs ( dp , a , b , m - 1 , n ) , lcs ( dp , a , b , m , n - 1 ) )
	dp [ m ] [ n ] = ans
	return ans
def costOfString ( s ) :
	cost = 0
	for i in range ( len ( s ) ) :
		cost += ( ord ( s [ i ] ) - 48 )
	return cost
if __name__ == " _ _ main _ _ " :
	a = "9142"
	b = "1429"
	dp = [ [ 0 for x in range ( 101 ) ] for y in range ( 101 ) ]
	print ( costOfString ( a ) + costOfString ( b ) - 2 * lcs ( dp , a , b , len ( a ) - 1 , len ( b ) - 1 ) )