Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
m,n = len(word1),len(word2)
ans = [[0 for i in range(n + 1)] for j in range(m + 1)]
for i in range(m + 1):
ans[i][n] = m - i
for i in range(n + 1):
ans[m][i] = n - i
m -= 1;n -= 1
while m >= 0:
t = n
while t >= 0:
if word1[m] == word2[t]:
ans[m][t] = ans[m + 1][t + 1]
else:
ans[m][t] = min(ans[m][t+1],ans[m+1][t],ans[m+1][t+1]) + 1
t -= 1
m -= 1
return ans[0][0]