From 72678661196950eea8def43e60a638f8a3b62ee9 Mon Sep 17 00:00:00 2001 From: Vinayak Jaiswal Date: Mon, 31 Oct 2022 23:19:16 +0530 Subject: [PATCH] adding edit distance --- dynamic_programing/Python/edit-distance.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 dynamic_programing/Python/edit-distance.py diff --git a/dynamic_programing/Python/edit-distance.py b/dynamic_programing/Python/edit-distance.py new file mode 100644 index 0000000000..5a8d54ff2c --- /dev/null +++ b/dynamic_programing/Python/edit-distance.py @@ -0,0 +1,20 @@ +class Solution: + def minDistance(self, word1: str, word2: str) -> int: + m=len(word1) + n=len(word2) + + dp=[[0 for y in range(n+1)]for x in range(m+1)] + + for i in range(m+1): + for j in range(n+1): + if i==0: + dp[i][j]=j + elif j==0: + dp[i][j]=i + else: + if word1[i-1]==word2[j-1]: + dp[i][j]=dp[i-1][j-1] + else: + dp[i][j]=1+ min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + + return dp[m][n] \ No newline at end of file