-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (36 loc) · 1.26 KB
/
Solution.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
38
package samoy.multidimensionaldp.editdistance;
import java.util.ArrayList;
import java.util.List;
/**
* @link <a href="https://leetcode.cn/problems/edit-distance/description/?envType=study-plan-v2&envId=top-100-liked">编辑距离</a>
*/
class Solution {
public int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
List<List<Integer>> op = new ArrayList<>();
for (int i = 0; i < len1 + 1; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j < len2 + 1; j++) {
if (i == 0) {
row.add(j);
} else if (j == 0) {
row.add(i);
} else {
row.add(0);
}
}
op.add(row);
}
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (word1.charAt(i) == word2.charAt(j)) {
op.get(i + 1).set(j + 1, op.get(i).get(j));
} else {
op.get(i + 1).set(j + 1, 1 + Math.min(Math.min(op.get(i + 1).get(j), op.get(i).get(j + 1)), op.get(i).get(j)));
}
}
}
return op.get(len1).get(len2);
}
}