-
Notifications
You must be signed in to change notification settings - Fork 7
/
MinimumEditDistanceDP.java
37 lines (31 loc) · 1.11 KB
/
MinimumEditDistanceDP.java
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
31
32
33
34
35
36
37
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class MinimumEditDistanceDP {
/*
This dp algorithm will tell you the minimum number of moves to convert one string to another
if the operations allowed are inserting a character, changing a character, and deleting a character.
*/
public int minimumEdit(String one, String two) {
int n = one.length(), m = two.length();
int[][] dp = new int[m+1][n+1];
for(int i = 1; i <= n; i++)
dp[0][i] = i;
for(int i = 1; i <= m; i++)
dp[i][0] = i;
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
char o = two.charAt(i-1), t = one.charAt(j-1);
if(o == t) {
dp[i][j] = dp[i-1][j-1];
} else {
dp[i][j] = Math.min(dp[i-1][j], Math.min(dp[i][j-1], dp[i-1][j-1])) + 1;
}
}
}
return dp[m][n];
}
public static void main(String[] args) throws Exception {
//Tested and worked
}
}