protected int getEditDistance(string a, string b)
{
if (a.Length == 0) return b.Length;
if (b.Length == 0) return a.Length;
var matrix = new int[b.Length+1, a.Length+1];
// increment along the first column of each row
int i;
for (i = 0; i <= b.Length; i++)
{
matrix[i, 0] = i;
}
// increment each column in the first row
int j;
for (j = 0; j <= a.Length; j++)
{
matrix[0, j] = j;
}
// Fill in the rest of the matrix
for (i = 1; i <= b.Length; i++)
{
for (j = 1; j <= a.Length; j++)
{
if (b[i - 1] == a[j - 1])
{
matrix[i, j] = matrix[i - 1, j - 1];
}
else
{
matrix[i, j] = Math.Min(matrix[i - 1, j - 1] + 1, // substitution
Math.Min(matrix[i, j - 1] + 1, // insertion
matrix[i - 1, j] + 1)); // deletion
}
}
}
return matrix[b.Length, a.Length];
}