class Solution: # @return an integer def minDistance(self, word1, word2): n=len(word1) m=len(word2) M=max(n,m) dp=[[M for i in range(m+1)] for j in range(n+1)] for i in range(n+1): dp[i][m]=n-i for i in range(m+1): dp[n][i]=m-i for i in reversed(range(n)): for j in reversed(range(m)): if word1[i]==word2[j]: 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[0][0]
Friday, September 12, 2014
Leetcode: Edit Distance @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment