File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s1
3
+ * @param {string } s2
4
+ * @return {number }
5
+ */
6
+ var minimumDeleteSum = function ( s1 , s2 ) {
7
+ const dp = [ [ ] ] ;
8
+ for ( let i = 0 ; i <= s1 . length ; i ++ ) {
9
+ dp [ i + 1 ] = [ ] ;
10
+ for ( let j = 0 ; j <= s2 . length ; j ++ ) {
11
+ if ( i === 0 && j === 0 ) {
12
+ dp [ i ] [ j ] = 0 ;
13
+ } else if ( i === 0 ) {
14
+ dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] || 0 ) + s2 . charCodeAt ( j - 1 ) ;
15
+ } else if ( j === 0 ) {
16
+ dp [ i ] [ j ] = ( ( dp [ i - 1 ] && dp [ i - 1 ] [ j ] ) || 0 ) + s1 . charCodeAt ( i - 1 ) ;
17
+ } else {
18
+ if ( s1 [ i - 1 ] === s2 [ j - 1 ] ) {
19
+ dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ;
20
+ } else {
21
+ dp [ i ] [ j ] = Math . min (
22
+ dp [ i - 1 ] [ j ] + s1 . charCodeAt ( i - 1 ) ,
23
+ dp [ i ] [ j - 1 ] + s2 . charCodeAt ( j - 1 ) ,
24
+ ) ;
25
+ }
26
+ }
27
+ }
28
+ }
29
+ return dp [ s1 . length ] [ s2 . length ] ;
30
+ } ;
You can’t perform that action at this time.
0 commit comments