46
46
47
47
** 方法一:动态规划**
48
48
49
- 类似 [ 1143. 最长公共子序列 ] ( /solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md ) 。
49
+ 我们定义 $f [ i ] [ j ] $ 表示使得 $s_1$ 的前 $i$ 个字符和 $s_2$ 的前 $j$ 个字符相等所需删除字符的 ASCII 值的最小和。那么答案就是 $f [ m ] [ n ] $ 。
50
50
51
- 定义 ` dp[i][j] ` 表示使得 ` s1[0: i-1]` 和 ` s2[0: j-1]` 两个字符串相等所需删除的字符的 ASCII 值的最小值。
51
+ 如果 $s_1 [ i-1 ] = s_2 [ j-1 ] $,那么 $f [ i ] [ j ] = f [ i-1] [ j-1 ] $。否则,我们可以删除 $s_1 [ i-1 ] $ 或者 $s_2 [ j-1] $ 中的一个,使得 $f [ i ] [ j ] $ 达到最小。因此,状态转移方程如下:
52
52
53
- 时间复杂度 O(mn)。
53
+ $$
54
+ f[i][j]=
55
+ \begin{cases}
56
+ f[i-1][j-1], & s_1[i-1] = s_2[j-1] \\
57
+ min(f[i-1][j] + s_1[i-1], f[i][j-1] + s_2[j-1]), & s_1[i-1] \neq s_2[j-1]
58
+ \end{cases}
59
+ $$
60
+
61
+ 初始状态为 $f[ 0] [ j ] = f[ 0] [ j-1 ] + s_2[ j-1] $,$f[ i] [ 0 ] = f[ i-1] [ 0 ] + s_1[ i-1] $。
62
+
63
+ 最后返回 $f[ m] [ n ] $ 即可。
64
+
65
+ 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是 $s_1$ 和 $s_2$ 的长度。
66
+
67
+ 相似题目:
68
+
69
+ - [ 1143. 最长公共子序列] ( /solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md )
54
70
55
71
<!-- tabs:start -->
56
72
62
78
class Solution :
63
79
def minimumDeleteSum (self , s1 : str , s2 : str ) -> int :
64
80
m, n = len (s1), len (s2)
65
- dp = [[0 ] * (n + 1 ) for _ in range (m + 1 )]
81
+ f = [[0 ] * (n + 1 ) for _ in range (m + 1 )]
66
82
for i in range (1 , m + 1 ):
67
- dp [i][0 ] = dp [i - 1 ][0 ] + ord (s1[i - 1 ])
83
+ f [i][0 ] = f [i - 1 ][0 ] + ord (s1[i - 1 ])
68
84
for j in range (1 , n + 1 ):
69
- dp [0 ][j] = dp [0 ][j - 1 ] + ord (s2[j - 1 ])
85
+ f [0 ][j] = f [0 ][j - 1 ] + ord (s2[j - 1 ])
70
86
for i in range (1 , m + 1 ):
71
87
for j in range (1 , n + 1 ):
72
88
if s1[i - 1 ] == s2[j - 1 ]:
73
- dp [i][j] = dp [i - 1 ][j - 1 ]
89
+ f [i][j] = f [i - 1 ][j - 1 ]
74
90
else :
75
- dp [i][j] = min (
76
- dp [i - 1 ][j] + ord (s1[i - 1 ]), dp [i][j - 1 ] + ord (s2[j - 1 ])
91
+ f [i][j] = min (
92
+ f [i - 1 ][j] + ord (s1[i - 1 ]), f [i][j - 1 ] + ord (s2[j - 1 ])
77
93
)
78
- return dp[ - 1 ][ - 1 ]
94
+ return f[m][n ]
79
95
```
80
96
81
97
### ** Java**
@@ -86,24 +102,23 @@ class Solution:
86
102
class Solution {
87
103
public int minimumDeleteSum (String s1 , String s2 ) {
88
104
int m = s1. length(), n = s2. length();
89
- int [][] dp = new int [m + 1 ][n + 1 ];
105
+ int [][] f = new int [m + 1 ][n + 1 ];
90
106
for (int i = 1 ; i <= m; ++ i) {
91
- dp [i][0 ] = dp [i - 1 ][0 ] + s1. codePointAt (i - 1 );
107
+ f [i][0 ] = f [i - 1 ][0 ] + s1. charAt (i - 1 );
92
108
}
93
109
for (int j = 1 ; j <= n; ++ j) {
94
- dp [0 ][j] = dp [0 ][j - 1 ] + s2. codePointAt (j - 1 );
110
+ f [0 ][j] = f [0 ][j - 1 ] + s2. charAt (j - 1 );
95
111
}
96
112
for (int i = 1 ; i <= m; ++ i) {
97
113
for (int j = 1 ; j <= n; ++ j) {
98
114
if (s1. charAt(i - 1 ) == s2. charAt(j - 1 )) {
99
- dp [i][j] = dp [i - 1 ][j - 1 ];
115
+ f [i][j] = f [i - 1 ][j - 1 ];
100
116
} else {
101
- dp[i][j] = Math . min(
102
- dp[i - 1 ][j] + s1. codePointAt(i - 1 ), dp[i][j - 1 ] + s2. codePointAt(j - 1 ));
117
+ f[i][j] = Math . min(f[i - 1 ][j] + s1. charAt(i - 1 ), f[i][j - 1 ] + s2. charAt(j - 1 ));
103
118
}
104
119
}
105
120
}
106
- return dp [m][n];
121
+ return f [m][n];
107
122
}
108
123
}
109
124
```
@@ -115,18 +130,24 @@ class Solution {
115
130
public:
116
131
int minimumDeleteSum(string s1, string s2) {
117
132
int m = s1.size(), n = s2.size();
118
- vector<vector<int >> dp(m + 1, vector<int >(n + 1));
119
- for (int i = 1; i <= m; ++i) dp[ i] [ 0 ] = dp[ i - 1] [ 0 ] + s1[ i - 1] ;
120
- for (int j = 1; j <= n; ++j) dp[ 0] [ j ] = dp[ 0] [ j - 1 ] + s2[ j - 1] ;
133
+ int f[ m + 1] [ n + 1 ] ;
134
+ memset(f, 0, sizeof f);
135
+ for (int i = 1; i <= m; ++i) {
136
+ f[ i] [ 0 ] = f[ i - 1] [ 0 ] + s1[ i - 1] ;
137
+ }
138
+ for (int j = 1; j <= n; ++j) {
139
+ f[ 0] [ j ] = f[ 0] [ j - 1 ] + s2[ j - 1] ;
140
+ }
121
141
for (int i = 1; i <= m; ++i) {
122
142
for (int j = 1; j <= n; ++j) {
123
- if (s1[ i - 1] == s2[ j - 1] )
124
- dp[ i] [ j ] = dp[ i - 1] [ j - 1 ] ;
125
- else
126
- dp[ i] [ j ] = min(dp[ i - 1] [ j ] + s1[ i - 1] , dp[ i] [ j - 1 ] + s2[ j - 1] );
143
+ if (s1[ i - 1] == s2[ j - 1] ) {
144
+ f[ i] [ j ] = f[ i - 1] [ j - 1 ] ;
145
+ } else {
146
+ f[ i] [ j ] = min(f[ i - 1] [ j ] + s1[ i - 1] , f[ i] [ j - 1 ] + s2[ j - 1] );
147
+ }
127
148
}
128
149
}
129
- return dp [ m] [ n ] ;
150
+ return f [ m] [ n ] ;
130
151
}
131
152
};
132
153
```
@@ -136,26 +157,26 @@ public:
136
157
```go
137
158
func minimumDeleteSum(s1 string, s2 string) int {
138
159
m, n := len(s1), len(s2)
139
- dp := make([][]int, m+1)
140
- for i := range dp {
141
- dp [i] = make([]int, n+1)
160
+ f := make([][]int, m+1)
161
+ for i := range f {
162
+ f [i] = make([]int, n+1)
142
163
}
143
- for i := 1; i <= m; i++ {
144
- dp[i ][0] = dp[i-1 ][0] + int(s1[i-1] )
164
+ for i, c := range s1 {
165
+ f[i+1 ][0] = f[i ][0] + int(c )
145
166
}
146
- for j := 1; j <= n; j++ {
147
- dp [0][j] = dp [0][j-1 ] + int(s2[j-1] )
167
+ for j, c := range s2 {
168
+ f [0][j+1 ] = f [0][j] + int(c )
148
169
}
149
170
for i := 1; i <= m; i++ {
150
171
for j := 1; j <= n; j++ {
151
172
if s1[i-1] == s2[j-1] {
152
- dp [i][j] = dp [i-1][j-1]
173
+ f [i][j] = f [i-1][j-1]
153
174
} else {
154
- dp [i][j] = min(dp [i-1][j]+int(s1[i-1]), dp [i][j-1]+int(s2[j-1]))
175
+ f [i][j] = min(f [i-1][j]+int(s1[i-1]), f [i][j-1]+int(s2[j-1]))
155
176
}
156
177
}
157
178
}
158
- return dp [m][n]
179
+ return f [m][n]
159
180
}
160
181
161
182
func min(a, b int) int {
@@ -166,6 +187,69 @@ func min(a, b int) int {
166
187
}
167
188
```
168
189
190
+ ### ** TypeScript**
191
+
192
+ ``` ts
193
+ function minimumDeleteSum(s1 : string , s2 : string ): number {
194
+ const m = s1 .length ;
195
+ const n = s2 .length ;
196
+ const f = Array .from ({ length: m + 1 }, () => Array (n + 1 ).fill (0 ));
197
+ for (let i = 1 ; i <= m ; ++ i ) {
198
+ f [i ][0 ] = f [i - 1 ][0 ] + s1 [i - 1 ].charCodeAt (0 );
199
+ }
200
+ for (let j = 1 ; j <= n ; ++ j ) {
201
+ f [0 ][j ] = f [0 ][j - 1 ] + s2 [j - 1 ].charCodeAt (0 );
202
+ }
203
+ for (let i = 1 ; i <= m ; ++ i ) {
204
+ for (let j = 1 ; j <= n ; ++ j ) {
205
+ if (s1 [i - 1 ] === s2 [j - 1 ]) {
206
+ f [i ][j ] = f [i - 1 ][j - 1 ];
207
+ } else {
208
+ f [i ][j ] = Math .min (
209
+ f [i - 1 ][j ] + s1 [i - 1 ].charCodeAt (0 ),
210
+ f [i ][j - 1 ] + s2 [j - 1 ].charCodeAt (0 ),
211
+ );
212
+ }
213
+ }
214
+ }
215
+ return f [m ][n ];
216
+ }
217
+ ```
218
+
219
+ ### ** JavaScript**
220
+
221
+ ``` js
222
+ /**
223
+ * @param {string} s1
224
+ * @param {string} s2
225
+ * @return {number}
226
+ */
227
+ var minimumDeleteSum = function (s1 , s2 ) {
228
+ const m = s1 .length ;
229
+ const n = s2 .length ;
230
+ const f = Array .from ({ length: m + 1 }, () => Array (n + 1 ).fill (0 ));
231
+ for (let i = 1 ; i <= m; ++ i) {
232
+ f[i][0 ] = f[i - 1 ][0 ] + s1[i - 1 ].charCodeAt (0 );
233
+ }
234
+ for (let j = 1 ; j <= n; ++ j) {
235
+ f[0 ][j] = f[0 ][j - 1 ] + s2[j - 1 ].charCodeAt (0 );
236
+ }
237
+ for (let i = 1 ; i <= m; ++ i) {
238
+ for (let j = 1 ; j <= n; ++ j) {
239
+ if (s1[i - 1 ] === s2[j - 1 ]) {
240
+ f[i][j] = f[i - 1 ][j - 1 ];
241
+ } else {
242
+ f[i][j] = Math .min (
243
+ f[i - 1 ][j] + s1[i - 1 ].charCodeAt (0 ),
244
+ f[i][j - 1 ] + s2[j - 1 ].charCodeAt (0 ),
245
+ );
246
+ }
247
+ }
248
+ }
249
+ return f[m][n];
250
+ };
251
+ ```
252
+
169
253
### ** ...**
170
254
171
255
```
0 commit comments