|
| 1 | + /** |
| 2 | + Author : SUBHAM SANGHAI |
| 3 | + A Dynamic Programming based solution for Edit Distance problem In Java |
| 4 | + **/ |
| 5 | + |
| 6 | + /**Description of Edit Distance with an Example: |
| 7 | +
|
| 8 | + Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another, |
| 9 | + by counting the minimum number of operations required to transform one string into the other. The |
| 10 | + distance operations are the removal, insertion, or substitution of a character in the string. |
| 11 | +
|
| 12 | +
|
| 13 | + The Distance between "kitten" and "sitting" is 3. A minimal edit script that transforms the former into the latter is: |
| 14 | +
|
| 15 | + kitten → sitten (substitution of "s" for "k") |
| 16 | + sitten → sittin (substitution of "i" for "e") |
| 17 | + sittin → sitting (insertion of "g" at the end).**/ |
| 18 | + |
| 19 | + import java.util.Scanner; |
| 20 | + public class Edit_Distance |
| 21 | + { |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + public static int minDistance(String word1, String word2) |
| 26 | + { |
| 27 | + int len1 = word1.length(); |
| 28 | + int len2 = word2.length(); |
| 29 | + // len1+1, len2+1, because finally return dp[len1][len2] |
| 30 | + int[][] dp = new int[len1 + 1][len2 + 1]; |
| 31 | + /* If second string is empty, the only option is to |
| 32 | + insert all characters of first string into second*/ |
| 33 | + for (int i = 0; i <= len1; i++) |
| 34 | + { |
| 35 | + dp[i][0] = i; |
| 36 | + } |
| 37 | + /* If first string is empty, the only option is to |
| 38 | + insert all characters of second string into first*/ |
| 39 | + for (int j = 0; j <= len2; j++) |
| 40 | + { |
| 41 | + dp[0][j] = j; |
| 42 | + } |
| 43 | + //iterate though, and check last char |
| 44 | + for (int i = 0; i < len1; i++) |
| 45 | + { |
| 46 | + char c1 = word1.charAt(i); |
| 47 | + for (int j = 0; j < len2; j++) |
| 48 | + { |
| 49 | + char c2 = word2.charAt(j); |
| 50 | + //if last two chars equal |
| 51 | + if (c1 == c2) |
| 52 | + { |
| 53 | + //update dp value for +1 length |
| 54 | + dp[i + 1][j + 1] = dp[i][j]; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + /* if two characters are different , |
| 59 | + then take the minimum of the various operations(i.e insertion,removal,substitution)*/ |
| 60 | + int replace = dp[i][j] + 1; |
| 61 | + int insert = dp[i][j + 1] + 1; |
| 62 | + int delete = dp[i + 1][j] + 1; |
| 63 | + |
| 64 | + int min = replace > insert ? insert : replace; |
| 65 | + min = delete > min ? min : delete; |
| 66 | + dp[i + 1][j + 1] = min; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + /* return the final answer , after traversing through both the strings*/ |
| 71 | + return dp[len1][len2]; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + // Driver program to test above function |
| 76 | + public static void main(String args[]) |
| 77 | + { |
| 78 | + Scanner input = new Scanner(System.in); |
| 79 | + String s1,s2; |
| 80 | + System.out.println("Enter the First String"); |
| 81 | + s1 = input.nextLine(); |
| 82 | + System.out.println("Enter the Second String"); |
| 83 | + s2 = input.nextLine(); |
| 84 | + //ans stores the final Edit Distance between the two strings |
| 85 | + int ans=0; |
| 86 | + ans=minDistance(s1,s2); |
| 87 | + System.out.println("The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 +"\" is "+ans); |
| 88 | + } |
| 89 | + } |
0 commit comments