forked from admirerr/DSA-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumEditDistance.java
More file actions
30 lines (28 loc) · 1.17 KB
/
MinimumEditDistance.java
File metadata and controls
30 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MinimumEditDistance {
public static void main(String[] args) {
String str1 = "kitten";
String str2 = "sitting";
System.out.println("Minimum Edit Distance between \"" + str1 + "\" and \"" + str2 + "\" is: " + minEditDistance(str1, str2));
}
public static int minEditDistance(String str1, String str2) {
int m = str1.length();
int n = str2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = j; // Insert all characters of str2
} else if (j == 0) {
dp[i][j] = i; // Remove all characters of str1
} else if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1]; // Characters match
} else {
dp[i][j] = 1 + Math.min(dp[i - 1][j], // Remove
Math.min(dp[i][j - 1], // Insert
dp[i - 1][j - 1])); // Replace
}
}
}
return dp[m][n];
}
}