int LevenshteinDistance(char s[1..m], char t[1..n]) { // for all i and j, d[i,j] will hold the Levenshtein distance between // the first i characters of s and the first j characters of t; // note that d has (m+1)x(n+1) values declare int d[0..m, 0..n]
fori from 0 to m d[i, 0] := i// the distance of any first string to an empty second string for j from 0 to n d[0, j] := j // the distance of any second string to an empty first string
for j from 1 to n { fori from 1 to m { if s[i] = t[j] then d[i, j] := d[i-1, j-1]// no operation required else d[i, j] := minimum ( d[i-1, j] + 1, // a deletion d[i, j-1] + 1, // an insertion d[i-1, j-1] + 1// a substitution ) } }
return d[m,n] }
【代码】
#Levenshtein Distance Algorithm #OS : Windows 7 #Python Version : Python 3.2 #IDE : VS2010 + PTVS
deflevenshtein(str1, str2): """ Levenshtein Distance Algorithm @param str1 string @param str2 string @return distance int """ #initialize dist dist = [[0for j inrange(len(str2) + 2)] for i inrange(len(str1) + 2)] for i inrange(len(str1) + 1): dist[i + 1][0] = i for j inrange(len(str2) + 1): dist[0][j + 1] = j for i inrange(len(str1)): for j inrange(len(str2)): if str1[i] == str2[j]: dist[i+1][j+1] = dist[i][j] else: dist[i+1][j+1] = min(dist[i][j+1], dist[i+1][j], dist[i][j]) + 1 return dist[len(str1)][len(str2)]