Skip to content

Commit 3f8e5df

Browse files
committed
72
1 parent ab3dcee commit 3f8e5df

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

DP/traditionalDP/72.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Edit Distance
2+
3+
#### Description
4+
5+
[link](https://zhuanlan.zhihu.com/p/80682302)
6+
7+
---
8+
9+
#### Solution
10+
11+
https://zhuanlan.zhihu.com/p/80682302
12+
13+
---
14+
15+
#### Code
16+
17+
> Complexity T : O(mn) M : O(mn)
18+
19+
```python
20+
class Solution:
21+
def minDistance(self, word1: str, word2: str) -> int:
22+
dp = [[float('inf') for _ in range(len(word2) + 1)] for _ in range(len(word1) + 1)]
23+
24+
for i in range(1 ,len(word1) + 1):
25+
dp[i][0] = i
26+
for j in range(1 ,len(word2) + 1):
27+
dp[0][j] = j
28+
29+
dp[0][0] = 0
30+
31+
for i in range(1, len(word1) + 1):
32+
for j in range(1, len(word2) + 1):
33+
if word1[i - 1] == word2[j - 1]:
34+
dp[i][j] = dp[i - 1][j - 1]
35+
else:
36+
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1)
37+
return dp[len(word1)][len(word2)]
38+
```

0 commit comments

Comments
 (0)