class Solution(object):
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """
        m=len(word1)
        n=len(word2)
        dp=[[0 for i in range(n+1)]for j in range(m+1)]
        for i in range(m+1):
            for j in range(n+1):
                if i ==0:
                    dp[i][j]=j
                elif j==0:
                    dp[i][j]=i
                elif word1[i-1]==word2[j-1]:
                    dp[i][j]=dp[i-1][j-1]
                else:
                    dp[i][j]=1+min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
        return dp[m][n]


word1="horse"
word2="ros"
s=Solution()
print(s.minDistance(word1,word2))
#use dp
#dp[i][j] 表示长度为i的word1和长度为j的word2distance
#word[i-1] 这里 因为长度为3的word最后一位是2