-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0072.java
More file actions
42 lines (40 loc) · 1.3 KB
/
Copy pathLeetCode0072.java
File metadata and controls
42 lines (40 loc) · 1.3 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
/* Edit Distance
* Input: word1 = "horse", word2 = "ros"
* Output: 3
* Explanation:
* horse -> rorse (replace 'h' with 'r')
* rorse -> rose (remove 'r')
* rose -> ros (remove 'e')
* */
public class LeetCode0072 {
public static void main(String args[]) {
String word1 = "horse";
String word2 = "ros";
System.out.println(minDistance(word1, word2));
}
/* # r o s
* # 0 1 2 3
* h 1 1 2 3
* o 2 2 1 2
* r 3 2 2 2
* s 4 3 3 2
* e 5 4 4 3
* */
public static int minDistance(String word1, String word2) {
int[][] dp = new int[word1.length() + 1][word2.length() + 1];
for (int i = 0; i <= word1.length(); i++) {
for (int j = 0; j <= word2.length(); j++) {
if (i == 0)
dp[i][j] = j;
else if (j == 0)
dp[i][j] = i;
else if (word1.charAt(i - 1) == word2.charAt(j - 1))
dp[i][j] = dp[i - 1][j - 1];
else
//行列表示增加或删除,对角线表示一次替换
dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
}
}
return dp[word1.length()][word2.length()];
}
}