Write a python function to find minimum edit distance between words given


Topic: Write a python function to find minimum edit distance between words given

Solution

def minDistance(word1, word2):
    m = len(word1)
    n = len(word2)
    if m*n == 0:
        return m + n
    d = [ [0] * (n + 1) for _ in range(m+1)]
    for i in range(m+1):
        d[i][0] = i
    for j in range(n+1):
            d[0][j] = j
    for i in range(m+1):
        for j in range(n+1):
            left = d[i-1][j] + 1
            down = d[i][j-1] + 1
            left_down = d[i-1][j-1]
            if word1[i-1] != word2[j-1]:
                left_down += 1
            d[i][j] = min(left, down, left_down)
    return d[m][n]
	
	



List all Python Programs