We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bb67ac3 commit cb6e852Copy full SHA for cb6e852
72-edit-distance.js
@@ -0,0 +1,25 @@
1
+/**
2
+ * @param {string} word1
3
+ * @param {string} word2
4
+ * @return {number}
5
+ */
6
+const minDistance = function(word1, word2) {
7
+ let m = word1.length, n = word2.length;
8
+ const dp = Array.from({length: m + 1}, ()=> new Array(n+ 1).fill(0))
9
+ for (let i = 1; i <= m; i++) {
10
+ dp[i][0] = i;
11
+ }
12
+ for (let j = 1; j <= n; j++) {
13
+ dp[0][j] = j;
14
15
16
17
+ if (word1[i - 1] === word2[j - 1]) {
18
+ dp[i][j] = dp[i - 1][j - 1];
19
+ } else {
20
+ dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i][j - 1], dp[i - 1][j])) + 1;
21
22
23
24
+ return dp[m][n];
25
+};
0 commit comments