Skip to content

Commit 645cfb7

Browse files
committed
+ problem 72
1 parent 54394da commit 645cfb7

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 72. Edit Distance
2+
Given two strings `word1` and `word2`, return *the minimum number of operations required to convert `word1` to `word2`*.
3+
4+
You have the following three operations permitted on a word:
5+
* Insert a character
6+
* Delete a character
7+
* Replace a character
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> word1 = "horse", word2 = "ros"
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong>
14+
horse -> rorse (replace 'h' with 'r')
15+
rorse -> rose (remove 'r')
16+
rose -> ros (remove 'e')
17+
</pre>
18+
19+
#### Example 2:
20+
<pre>
21+
<strong>Input:</strong> word1 = "intention", word2 = "execution"
22+
<strong>Output:</strong> 5
23+
<strong>Explanation:</strong>
24+
intention -> inention (remove 't')
25+
inention -> enention (replace 'i' with 'e')
26+
enention -> exention (replace 'n' with 'x')
27+
exention -> exection (replace 'n' with 'c')
28+
exection -> execution (insert 'u')
29+
</pre>
30+
31+
#### Constraints:
32+
* `0 <= word1.length, word2.length <= 500`
33+
* `word1` and `word2` consist of lowercase English letters.
34+
35+
## Solutions (Rust)
36+
37+
### 1. Solution
38+
```Rust
39+
impl Solution {
40+
pub fn min_distance(word1: String, word2: String) -> i32 {
41+
let word1 = word1.as_bytes();
42+
let word2 = word2.as_bytes();
43+
let mut dp = vec![vec![i32::MAX; word2.len() + 1]; word1.len() + 1];
44+
dp[0] = (0..=word2.len() as i32).collect();
45+
46+
for i in 1..=word1.len() {
47+
dp[i][0] = i as i32;
48+
for j in 1..=word2.len() {
49+
if word1[i - 1] == word2[j - 1] {
50+
dp[i][j] = dp[i][j].min(dp[i - 1][j - 1]);
51+
}
52+
dp[i][j] = dp[i][j]
53+
.min(dp[i][j - 1] + 1)
54+
.min(dp[i - 1][j] + 1)
55+
.min(dp[i - 1][j - 1] + 1);
56+
}
57+
}
58+
59+
dp[word1.len()][word2.len()]
60+
}
61+
}
62+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 72. 编辑距离
2+
给你两个单词 `word1``word2`*请返回将 `word1` 转换成 `word2` 所使用的最少操作数*
3+
4+
你可以对一个单词进行如下三种操作:
5+
* 插入一个字符
6+
* 删除一个字符
7+
* 替换一个字符
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> word1 = "horse", word2 = "ros"
12+
<strong>输出:</strong> 3
13+
<strong>解释:</strong>
14+
horse -> rorse (将 'h' 替换为 'r')
15+
rorse -> rose (删除 'r')
16+
rose -> ros (删除 'e')
17+
</pre>
18+
19+
#### 示例 2:
20+
<pre>
21+
<strong>输入:</strong> word1 = "intention", word2 = "execution"
22+
<strong>输出:</strong> 5
23+
<strong>解释:</strong>
24+
intention -> inention (删除 't')
25+
inention -> enention (将 'i' 替换为 'e')
26+
enention -> exention (将 'n' 替换为 'x')
27+
exention -> exection (将 'n' 替换为 'c')
28+
exection -> execution (插入 'u')
29+
</pre>
30+
31+
#### 提示:
32+
* `0 <= word1.length, word2.length <= 500`
33+
* `word1``word2` 由小写英文字母组成
34+
35+
## 题解 (Rust)
36+
37+
### 1. 题解
38+
```Rust
39+
impl Solution {
40+
pub fn min_distance(word1: String, word2: String) -> i32 {
41+
let word1 = word1.as_bytes();
42+
let word2 = word2.as_bytes();
43+
let mut dp = vec![vec![i32::MAX; word2.len() + 1]; word1.len() + 1];
44+
dp[0] = (0..=word2.len() as i32).collect();
45+
46+
for i in 1..=word1.len() {
47+
dp[i][0] = i as i32;
48+
for j in 1..=word2.len() {
49+
if word1[i - 1] == word2[j - 1] {
50+
dp[i][j] = dp[i][j].min(dp[i - 1][j - 1]);
51+
}
52+
dp[i][j] = dp[i][j]
53+
.min(dp[i][j - 1] + 1)
54+
.min(dp[i - 1][j] + 1)
55+
.min(dp[i - 1][j - 1] + 1);
56+
}
57+
}
58+
59+
dp[word1.len()][word2.len()]
60+
}
61+
}
62+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn min_distance(word1: String, word2: String) -> i32 {
3+
let word1 = word1.as_bytes();
4+
let word2 = word2.as_bytes();
5+
let mut dp = vec![vec![i32::MAX; word2.len() + 1]; word1.len() + 1];
6+
dp[0] = (0..=word2.len() as i32).collect();
7+
8+
for i in 1..=word1.len() {
9+
dp[i][0] = i as i32;
10+
for j in 1..=word2.len() {
11+
if word1[i - 1] == word2[j - 1] {
12+
dp[i][j] = dp[i][j].min(dp[i - 1][j - 1]);
13+
}
14+
dp[i][j] = dp[i][j]
15+
.min(dp[i][j - 1] + 1)
16+
.min(dp[i - 1][j] + 1)
17+
.min(dp[i - 1][j - 1] + 1);
18+
}
19+
}
20+
21+
dp[word1.len()][word2.len()]
22+
}
23+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
[69][69l] |[Sqrt(x)][69] |![rs]
7070
[70][70l] |[Climbing Stairs][70] |![rs]
7171
[71][71l] |[Simplify Path][71] |![py]
72+
[72][72l] |[Edit Distance][72] |![rs]
7273
[73][73l] |[Set Matrix Zeroes][73] |![rs]
7374
[74][74l] |[Search a 2D Matrix][74] |![rs]
7475
[75][75l] |[Sort Colors][75] |![rs]
@@ -1657,6 +1658,7 @@
16571658
[69]:Problemset/0069-Sqrt\(x\)/README.md#69-sqrtx
16581659
[70]:Problemset/0070-Climbing%20Stairs/README.md#70-climbing-stairs
16591660
[71]:Problemset/0071-Simplify%20Path/README.md#71-simplify-path
1661+
[72]:Problemset/0072-Edit%20Distance/README.md#72-edit-distance
16601662
[73]:Problemset/0073-Set%20Matrix%20Zeroes/README.md#73-set-matrix-zeroes
16611663
[74]:Problemset/0074-Search%20a%202D%20Matrix/README.md#74-search-a-2d-matrix
16621664
[75]:Problemset/0075-Sort%20Colors/README.md#75-sort-colors
@@ -3237,6 +3239,7 @@
32373239
[69l]:https://leetcode.com/problems/sqrtx/
32383240
[70l]:https://leetcode.com/problems/climbing-stairs/
32393241
[71l]:https://leetcode.com/problems/simplify-path/
3242+
[72l]:https://leetcode.com/problems/edit-distance/
32403243
[73l]:https://leetcode.com/problems/set-matrix-zeroes/
32413244
[74l]:https://leetcode.com/problems/search-a-2d-matrix/
32423245
[75l]:https://leetcode.com/problems/sort-colors/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
[69][69l] |[x 的平方根][69] |![rs]
7070
[70][70l] |[爬楼梯][70] |![rs]
7171
[71][71l] |[简化路径][71] |![py]
72+
[72][72l] |[编辑距离][72] |![rs]
7273
[73][73l] |[矩阵置零][73] |![rs]
7374
[74][74l] |[搜索二维矩阵][74] |![rs]
7475
[75][75l] |[颜色分类][75] |![rs]
@@ -1657,6 +1658,7 @@
16571658
[69]:Problemset/0069-Sqrt\(x\)/README_CN.md#69-x-的平方根
16581659
[70]:Problemset/0070-Climbing%20Stairs/README_CN.md#70-爬楼梯
16591660
[71]:Problemset/0071-Simplify%20Path/README_CN.md#71-简化路径
1661+
[72]:Problemset/0072-Edit%20Distance/README_CN.md#72-编辑距离
16601662
[73]:Problemset/0073-Set%20Matrix%20Zeroes/README_CN.md#73-矩阵置零
16611663
[74]:Problemset/0074-Search%20a%202D%20Matrix/README_CN.md#74-搜索二维矩阵
16621664
[75]:Problemset/0075-Sort%20Colors/README_CN.md#75-颜色分类
@@ -3237,6 +3239,7 @@
32373239
[69l]:https://leetcode.cn/problems/sqrtx/
32383240
[70l]:https://leetcode.cn/problems/climbing-stairs/
32393241
[71l]:https://leetcode.cn/problems/simplify-path/
3242+
[72l]:https://leetcode.cn/problems/edit-distance/
32403243
[73l]:https://leetcode.cn/problems/set-matrix-zeroes/
32413244
[74l]:https://leetcode.cn/problems/search-a-2d-matrix/
32423245
[75l]:https://leetcode.cn/problems/sort-colors/

0 commit comments

Comments
 (0)