Skip to content

Commit 1be62bc

Browse files
committed
Add solution #583
1 parent 0672d26 commit 1be62bc

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@
454454
575|[Distribute Candies](./0575-distribute-candies.js)|Easy|
455455
576|[Out of Boundary Paths](./0576-out-of-boundary-paths.js)|Medium|
456456
581|[Shortest Unsorted Continuous Subarray](./0581-shortest-unsorted-continuous-subarray.js)|Medium|
457+
583|[Delete Operation for Two Strings](./0583-delete-operation-for-two-strings.js)|Medium|
457458
589|[N-ary Tree Preorder Traversal](./0589-n-ary-tree-preorder-traversal.js)|Easy|
458459
594|[Longest Harmonious Subsequence](./0594-longest-harmonious-subsequence.js)|Easy|
459460
599|[Minimum Index Sum of Two Lists](./0599-minimum-index-sum-of-two-lists.js)|Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 583. Delete Operation for Two Strings
3+
* https://leetcode.com/problems/delete-operation-for-two-strings/
4+
* Difficulty: Medium
5+
*
6+
* Given two strings word1 and word2, return the minimum number of steps required to
7+
* make word1 and word2 the same.
8+
*
9+
* In one step, you can delete exactly one character in either string.
10+
*/
11+
12+
/**
13+
* @param {string} word1
14+
* @param {string} word2
15+
* @return {number}
16+
*/
17+
var minDistance = function(word1, word2) {
18+
const dp = new Array(word1.length + 1).fill().map(() => {
19+
return new Array(word2.length + 1).fill(0);
20+
});
21+
22+
for (let i = 1; i <= word1.length; i++) {
23+
dp[i][0] = i;
24+
}
25+
for (let j = 1; j <= word2.length; j++) {
26+
dp[0][j] = j;
27+
}
28+
for (let i = 1; i <= word1.length; i++) {
29+
for (let j = 1; j <= word2.length; j++) {
30+
dp[i][j] = word1[i - 1] === word2[j - 1]
31+
? dp[i - 1][j - 1]
32+
: Math.min(dp[i - 1][j], dp[i][j - 1]) + 1;
33+
}
34+
}
35+
36+
return dp[word1.length][word2.length];
37+
};

0 commit comments

Comments
 (0)