Skip to content

Commit aafdbbc

Browse files
authored
Create Edit Distance
1 parent 1eaf966 commit aafdbbc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Edit Distance

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int minDistance(String word1, String word2) {
3+
int[][] dp = new int[word1.length()+1][word2.length()+1];
4+
for(int i = 0; i<dp.length; i++){
5+
for(int j = 0; j<dp[i].length; j++){
6+
if(i == 0){
7+
dp[i][j] = j;
8+
}
9+
else if(j == 0){
10+
dp[i][j] = i;
11+
}
12+
else if(word1.charAt(i-1) == word2.charAt(j-1)){
13+
dp[i][j] = dp[i-1][j-1];
14+
}
15+
else{
16+
dp[i][j] = Math.min(dp[i][j-1], Math.min(dp[i-1][j], dp[i-1][j-1]))+1;
17+
}
18+
}
19+
}
20+
return dp[word1.length()][word2.length()];
21+
}
22+
}

0 commit comments

Comments
 (0)