Skip to content

Commit 37a0753

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update 72. Edit Distance.py
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent 9f58758 commit 37a0753

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def minDistance(self, word1: str, word2: str) -> int:
3+
m, n = len(word1), len(word2)
4+
f = [[0] * (n + 1) for _ in range(m + 1)]
5+
for j in range(1, n + 1):
6+
f[0][j] = j
7+
for i, a in enumerate(word1, 1):
8+
f[i][0] = i
9+
for j, b in enumerate(word2, 1):
10+
if a == b:
11+
f[i][j] = f[i - 1][j - 1]
12+
else:
13+
f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
14+
return f[m][n]

0 commit comments

Comments
 (0)