forked from rubythonode/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-operation-for-two-strings.js
53 lines (46 loc) · 1.16 KB
/
delete-operation-for-two-strings.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Delete Operation for Two Strings
*
* Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same,
* where in each step you can delete one character in either string.
*
* Example 1:
*
* Input: "sea", "eat"
* Output: 2
*
* Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
*
* Note:
* The length of given words won't exceed 500.
* Characters in given words can only be lower-case letters.
*/
/**
* @param {string} word1
* @param {string} word2
* @return {number}
*/
const minDistance = (word1, word2) => {
const m = word1.length;
const n = word2.length;
const dp = Array(m + 1)
.fill()
.map(() => Array(n + 1).fill(0));
for (let i = 0; i <= m; i++) {
dp[i][0] = i;
}
for (let j = 0; j <= n; j++) {
dp[0][j] = j;
}
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (word1[i - 1] === word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i - 1][j - 1] + 2, Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1));
}
}
}
return dp[m][n];
};
export { minDistance };