Created
July 23, 2013 06:08
-
-
Save riyadparvez/6060204 to your computer and use it in GitHub Desktop.
Computes edit distance recursively between to string in C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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