forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditdis.java
More file actions
executable file
·25 lines (25 loc) · 806 Bytes
/
editdis.java
File metadata and controls
executable file
·25 lines (25 loc) · 806 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
public class Solution {
public int minDistance(String word1, String word2) {
// Start typing your Java solution below
// DO NOT write main() function
int t1 = word1.length();
int t2 = word2.length();
int[][] k= new int[t1+1][t2+1];
for(int i=0;i<t1+1;i++){
k[i][0] = i;
}
for(int i=0;i<t2+1;i++){
k[0][i] = i;
}
for(int i=1;i<t1+1;i++){
for(int j=1;j<t2+1;j++){
if(word1.charAt(i-1)==word2.charAt(j-1)){
k[i][j]=k[i-1][j-1];
}else{
k[i][j] = Math.min(k[i-1][j-1]+1, k[i][j-1]+1);
k[i][j] = Math.min(k[i][j],k[i-1][j]+1);
}
}
}
return k[t1][t2];
}