File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 454
454
575|[ Distribute Candies] ( ./0575-distribute-candies.js ) |Easy|
455
455
576|[ Out of Boundary Paths] ( ./0576-out-of-boundary-paths.js ) |Medium|
456
456
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|
457
458
589|[ N-ary Tree Preorder Traversal] ( ./0589-n-ary-tree-preorder-traversal.js ) |Easy|
458
459
594|[ Longest Harmonious Subsequence] ( ./0594-longest-harmonious-subsequence.js ) |Easy|
459
460
599|[ Minimum Index Sum of Two Lists] ( ./0599-minimum-index-sum-of-two-lists.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments