Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save riyadparvez/6060204 to your computer and use it in GitHub Desktop.
Computes edit distance recursively between to string in C#.
public static int EditDistanceRecursive(string str1, string str2, int m, int n)
{
if(m < 0 || n < 0 ||
m >= str1.Length ||
n >= str2.Length)
{
throw new ArgumentOutOfRangeException();
}
if(m == 0 && n == 0)
{
return 0;
}
if(m == 0)
{
return n;
}
if(n == 0)
{
return m;
}
int left = EditDistanceRecursive(str1, str2, m-1, n) + 1;
int right = EditDistanceRecursive(str1, str2, m, n - 1) + 1;
int middle = EditDistanceRecursive(str1, str2, m - 1, n - 1) + ((str1[m] == str2[n]) ? 0 : 1);
return Min(Min(left, right), middle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment