#!/usr/bin/env python3


class Solution:
    def minDistance(self, word1, word2):
        """
        :type word1: str
        :type word2: str
        :rtype: int
        """

        len1 = len(word1)
        len2 = len(word2)
        dp = []
        for i in range(len1 + 1):
            dp.append((len2 + 1) * [0])
        for i in range(len1 + 1):
            dp[i][0] = i
        for j in range(len2 + 1):
            dp[0][j] = j

        for i in range(1, len1 + 1):
            for j in range(1, len2 + 1):
                if word1[i - 1] == word2[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1]
                else:
                    delete = dp[i - 1][j]
                    insert = dp[i][j - 1]
                    replace = dp[i - 1][j - 1]
                    dp[i][j] = min(delete, insert, replace) + 1

        return dp[len1][len2]


if __name__ == '__main__':
    s = Solution()
    print(s.minDistance('', 'a'))
    print(s.minDistance('asdf', 'asdff'))
    print(s.minDistance('asd', 'asdff'))
    print(s.minDistance('asdd', 'asdff'))
