-
Notifications
You must be signed in to change notification settings - Fork 0
/
72.编辑距离.js
38 lines (34 loc) · 972 Bytes
/
72.编辑距离.js
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
/**
* @param {string} word1
* @param {string} word2
* @return {number}
* https://leetcode-cn.com/problems/edit-distance/
*/
var minDistance = function(s1, s2) {
// dp[i][j] 存储 s1[0 ~ i-1] 变为 s2[0 ~ j-1] 的最小编辑距离
const memo = new Array(s1.length + 1).fill(1);
memo.forEach((el, i) => memo[i] = new Array(s2.length + 1))
for (let out = 0; out < memo.length; out++) {
memo[out][0] = out
}
for (let inn = 0; inn < memo[0].length; inn++) {
memo[0][inn] = inn
}
// 从下往上遍历
for (let i = 1; i <= s1.length; i++) {
for (let j = 1; j <= s2.length; j++) {
if (s1[i-1] === s2[j-1]) {
memo[i][j] = memo[i-1][j-1];
} else {
memo[i][j] = 1 + Math.min(
memo[i-1][j-1],
memo[i-1][j],
memo[i][j-1]
)
}
}
}
return memo[s1.length][s2.length];
};
minDistance("horse", "ros");
// minDistance("dinitrophenylhydrazine", "benzalphenylhydrazone");