def minDistance(self, word1: str, word2: str) -> int:
n = len(word1)
m = len(word2)
# ----求最长公共子序列LCS(Longest Common Subsequence)的长度
dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)] # 虚指
for i in range(1, n + 1):
for j in range(1, m + 1):
if word1[i - 1] == word2[j - 1]: # 转成实指
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
LCSn = dp[n][m]
return (n - LCSn) + (m - LCSn)