We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c504451 commit 8e67bf6Copy full SHA for 8e67bf6
code/72.edit-distance.py
@@ -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