Skip to content

Instantly share code, notes, and snippets.

@riyadparvez
Created July 23, 2013 06:19
Show Gist options
  • Select an option

  • Save riyadparvez/6060224 to your computer and use it in GitHub Desktop.

Select an option

Save riyadparvez/6060224 to your computer and use it in GitHub Desktop.
Computes edit distance using dynamic programming in C#.
public static int EditDistance(string str1, string str2)
{
int[,] distance = new int[str1.Length+1, str2.Length+1];
for (int i = 0; i <= str1.Length; i++)
{
distance[i, 0] = i;
}
for (int i = 0; i <= str2.Length; i++)
{
distance[0, i] = i;
}
for (int i = 1; i <= str1.Length; i++)
{
for (int j = 1; j <= str2.Length; j++)
{
distance[i, j] = Min(Min(distance[i-1, j], distance[i, j-1]),
distance[i-1, j-1] + (str1[i-1] == str2[j-1] ? 0 : 1));
}
}
return distance[str1.Length, str2.Length];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment