Update 0583.两个字符串的删除操作.md

This commit is contained in:
fwqaaq
2023-07-23 20:00:17 +08:00
committed by GitHub
parent 0a83ee2c20
commit d6bf0c52ca

View File

@ -394,6 +394,26 @@ impl Solution {
}
```
> 版本 2
```rust
impl Solution {
pub fn min_distance(word1: String, word2: String) -> i32 {
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
for (i, char1) in word1.chars().enumerate() {
for (j, char2) in word2.chars().enumerate() {
if char1 == char2 {
dp[i + 1][j + 1] = dp[i][j] + 1;
continue;
}
dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]);
}
}
(word1.len() + word2.len() - 2 * dp[word1.len()][word2.len()]) as i32
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">