File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 72. Edit Distance
3
+ * https://leetcode.com/problems/edit-distance/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given two strings word1 and word2, return the minimum number of operations required to
7
+ * convert word1 to word2.
8
+ *
9
+ * You have the following three operations permitted on a word:
10
+ * - Insert a character
11
+ * - Delete a character
12
+ * - Replace a character
13
+ */
14
+
15
+ /**
16
+ * @param {string } word1
17
+ * @param {string } word2
18
+ * @return {number }
19
+ */
20
+ var minDistance = function ( word1 , word2 ) {
21
+ const cache = new Array ( word1 . length + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( word2 . length + 1 ) ) ;
22
+
23
+ for ( let i = 0 ; i <= word1 . length ; i ++ ) {
24
+ for ( let j = 0 ; j <= word2 . length ; j ++ ) {
25
+ if ( i === 0 ) {
26
+ cache [ 0 ] [ j ] = j ;
27
+ } else if ( j === 0 ) {
28
+ cache [ i ] [ 0 ] = i ;
29
+ } else if ( word1 [ i - 1 ] == word2 [ j - 1 ] ) {
30
+ cache [ i ] [ j ] = cache [ i - 1 ] [ j - 1 ] ;
31
+ } else {
32
+ cache [ i ] [ j ] = Math . min ( cache [ i ] [ j - 1 ] , cache [ i - 1 ] [ j - 1 ] , cache [ i - 1 ] [ j ] ) + 1 ;
33
+ }
34
+ }
35
+ }
36
+
37
+ return cache [ word1 . length ] [ word2 . length ] ;
38
+ } ;
Original file line number Diff line number Diff line change 75
75
69|[ Sqrt(x)] ( ./0069-sqrtx.js ) |Medium|
76
76
70|[ Climbing Stairs] ( ./0070-climbing-stairs.js ) |Easy|
77
77
71|[ Simplify Path] ( ./0071-simplify-path.js ) |Medium|
78
+ 72|[ Edit Distance] ( ./0072-edit-distance.js ) |Medium|
78
79
73|[ Set Matrix Zeroes] ( ./0073-set-matrix-zeroes.js ) |Medium|
79
80
74|[ Search a 2D Matrix] ( ./0074-search-a-2d-matrix.js ) |Medium|
80
81
75|[ Sort Colors] ( ./0075-sort-colors.js ) |Medium|
You can’t perform that action at this time.
0 commit comments