Created
July 23, 2013 06:19
-
-
Save riyadparvez/6060224 to your computer and use it in GitHub Desktop.
Computes edit distance using dynamic programming 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 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