-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.java
More file actions
29 lines (24 loc) · 942 Bytes
/
EditDistance.java
File metadata and controls
29 lines (24 loc) · 942 Bytes
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
public class EditDistance {
public int minDistance(String word1, String word2) {
// retkkkk peta
int len1 = word1.length();
int len2 = word2.length();
int[][] dpRet = new int[len1+1][len2+1];
for(int i=0; i<=len1; i++) dpRet[i][0] = i;
for(int i=0; i<=len2; i++) dpRet[0][i] = i;
for(int i=0; i<len1; i++) {
char c1 = word1.charAt(i);
for(int j=0; j<len2; j++) {
if(c1 == word2.charAt(j))
dpRet[i+1][j+1] = dpRet[i][j];
else {
int insert = dpRet[i+1][j] + 1;
int delete = dpRet[i][j+1] + 1;
int replace = dpRet[i][j] + 1;
dpRet[i+1][j+1] = Math.min(insert, Math.min(delete, replace));
}
}
}
return dpRet[len1][len2];
}
}