-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.java
More file actions
54 lines (51 loc) · 1.5 KB
/
EditDistance.java
File metadata and controls
54 lines (51 loc) · 1.5 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class EditDistance {
public int minDistance(String word1, String word2) {
int x = word1.length();
int y = word2.length();
if (x == 0) {
return y;
}
if (y == 0) {
return x;
}
int [][] mat = new int[x + 1][y + 1];
for (int i = 0; i <= x; i++) {
mat[i][0] = i;
}
for (int i = 0; i <= y; i++) {
mat[0][i] = i;
}
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
int insert = mat[i][j - 1] + 1;
int delete = mat[i - 1][j] + 1;
int replace = mat[i - 1][j - 1];
//System.out.println("replace = " + replace);
if (word1.charAt(i - 1) != word2.charAt(j - 1)) {
//System.out.println("if");
replace++;
}
//System.out.println(i + ", " + j + ": " + word1.charAt(i - 1) + ", " + word2.charAt(j - 1) + "; insert = " + insert + "; delete = " + delete + "; replace = "+ replace);
mat[i][j] = Math.min(insert, Math.min(delete, replace));
}
}
printMat(mat);
return mat[x][y];
}
public void printMat(int[][] mat) {
int x = mat.length;
int y = mat[0].length;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println(" ");
}
System.out.println(" ");
}
public void test() {
String word1 = "ab";
String word2 = "bc";
System.out.println(minDistance(word1, word2));
}
}