Skip to content

Commit 8e67bf6

Browse files
committed
72
1 parent c504451 commit 8e67bf6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

code/72.edit-distance.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# @lc app=leetcode id=72 lang=python3
3+
#
4+
# [72] Edit Distance
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def minDistance(self, word1: str, word2: str) -> int:
10+
dp = [[float('inf') for _ in range(len(word2) + 1)] for _ in range(len(word1) + 1)]
11+
12+
for i in range(1 ,len(word1) + 1):
13+
dp[i][0] = i
14+
for j in range(1 ,len(word2) + 1):
15+
dp[0][j] = j
16+
17+
dp[0][0] = 0
18+
19+
for i in range(1, len(word1) + 1):
20+
for j in range(1, len(word2) + 1):
21+
if word1[i - 1] == word2[j - 1]:
22+
dp[i][j] = dp[i - 1][j - 1]
23+
else:
24+
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1)
25+
return dp[len(word1)][len(word2)]
26+
# @lc code=end
27+

0 commit comments

Comments
 (0)