class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
if len(word1)==0:
return len(word2)
if len(word2)==0:
return len(word1)
step={}
i,j=0,0
for i in xrange(len(word1)):
for j in xrange(len(word2)):
opt=0
match=0
ss=0
if word1[i]==word2[j]:
match=1
if i==0 and j==0:
opt=1
ss=1-match
elif i==0:
if step[(i,j-1)]!=j:
ss=1+step[(i,j-1)]
else:
ss=1-match+step[(i,j-1)]
opt=1
elif j==0:
if step[(i-1,j)]!=i:
ss=1+step[(i-1,j)]
else:
ss=1-match+step[(i-1,j)]
if i>0 and j>0:
ss=min(1-match+step[(i-1,j-1)],1+step[(i-1,j)],1+step[(i,j-1)])
step[(i,j)]=ss
return step[(len(word1)-1,len(word2)-1)]