动态规划
base:第0行和第0列
状态转移: 因为要考虑空串也就是base case的情况,所以会造成字符串一位的偏移
dp[i-1][j] + ord(s1[i-1]) 说明上往下移,也就是当前行的字符串被删除了,因为字符串的一位偏移,所以减去ord(s1[i-1])
dp[i][j-1] + ord(s2[j-1]) 说明左往右移,也就是当前列的字符串被删除了
def minimumDeleteSum(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: int
"""
m,n = len(s1),len(s2)
dp = [[0]*(n+1) for i in range(m+1)]
for i in range(1,m+1):
dp[i][0] = dp[i-1][0] + ord(s1[i-1])
for j in range(1,n+1):
dp[0][j] =dp[0][j-1] + ord(s2[j-1])
for i in range(1,m+1):
for j in range(1,n+1):
if s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j] + ord(s1[i-1]), dp[i][j-1] + ord(s2[j-1]))#
return dp[-1][-1]