-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path72._Edit_Distance.java
37 lines (27 loc) · 1.2 KB
/
72._Edit_Distance.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
// Problem Link
// https://leetcode.com/problems/edit-distance/
public class Solution {
int[][] dp;
public int minDistance(String word1, String word2) {
dp = new int[word1.length()][word2.length()];
return minDistanceHelper(word1, word2, 0, 0);
}
private int minDistanceHelper(String word1, String word2, int index1, int index2) {
if (index1 == word1.length()) return word2.length() - index2;
if (index2 == word2.length()) return word1.length() - index1;
if (dp[index1][index2] > 0) return dp[index1][index2];
int result;
if (word1.charAt(index1) == word2.charAt(index2)) {
result = minDistanceHelper(word1, word2, index1+1, index2+1);
} else {
// replace char
result = 1 + minDistanceHelper(word1, word2, index1+1, index2+1);
// delete char from word1
result = Math.min(result, 1 + minDistanceHelper(word1, word2, index1+1, index2));
// delete char from word2
result = Math.min(result, 1 + minDistanceHelper(word1, word2, index1, index2+1));
}
dp[index1][index2] = result;
return result;
}
}