From 2c4f24435ff9641a27a275ae53a91d871b79a682 Mon Sep 17 00:00:00 2001 From: Kumar Laxmikant Date: Thu, 3 Oct 2024 22:08:58 +0200 Subject: [PATCH] Pushing Edit Distance algorithm in Python --- Python/Dynamic-Programming/edit_distance.py | 33 +++++++++++++++++++++ README.md | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Python/Dynamic-Programming/edit_distance.py diff --git a/Python/Dynamic-Programming/edit_distance.py b/Python/Dynamic-Programming/edit_distance.py new file mode 100644 index 000000000..7d7317e49 --- /dev/null +++ b/Python/Dynamic-Programming/edit_distance.py @@ -0,0 +1,33 @@ +def editDistRec(s1, s2, m, n): + # If first string is empty, the only option is to + # insert all characters of second string into first + if m == 0: + return n + + # If second string is empty, the only option is to + # remove all characters of first string + if n == 0: + return m + + # If last characters of two strings are same, nothing + # much to do. Get the count for + # remaining strings. + if s1[m - 1] == s2[n - 1]: + return editDistRec(s1, s2, m - 1, n - 1) + + # If last characters are not same, consider all three + # operations on last character of first string, + # recursively compute minimum cost for all three + # operations and take minimum of three values. + return 1 + min(editDistRec(s1, s2, m, n - 1), # Insert + editDistRec(s1, s2, m - 1, n), # Remove + editDistRec(s1, s2, m - 1, n - 1) # Replace + ) + +def editDist(s1, s2): + return editDistRec(s1, s2, len(s1), len(s2)) + +# Driver code +s1 = "GEEXSFRGEEKKS" +s2 = "GEEKSFORGEEKS" +print(editDist(s1, s2)) \ No newline at end of file diff --git a/README.md b/README.md index 09b374201..59ebd5207 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ | --- | ----------------------------------------------------------------------------------- | ------------------------------ | | 1. | 0/1 Knapsack | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Dynamic-Programming/01_knapsack.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Dynamic-Programming/01_knapsack.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Dynamic-Programming/_01_knapsack.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Dynamic-Programming/01_knapsack.py) | | 2. | Coin change problem | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Dynamic-Programming/coinchange.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Dynamic-Programming/coinchange.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Dynamic-Programming/coinchange.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Dynamic-Programming/coinchange.py) | -| 3. | Edit Distance | [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Dynamic-Programming/edit_distance.cpp) | +| 3. | Edit Distance | [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Dynamic-Programming/edit_distance.cpp), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Dynamic-Programming/edit_distance.py) | | 4. | Fibonacci Number | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Dynamic-Programming/Fibonacci_Number.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Dynamic-Programming/Fibonacci_Number.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Dynamic-Programming/Fibonacci_Number.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Dynamic-Programming/Fibonacci_Number.py) | | 5. | Hierholzer Algorithm | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Graphs/Hierholzer_Algorithm.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Graphs/Hierholzer_Algorithm.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Graphs/Hierholzer_Algorithm.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Graphs/Hierholzer_Algorithm.py) | | 6. | Longest Common Subsequence | [C](https://github.com/Kumar-laxmi/Algorithms/blob/main/C/Dynamic-Programming/Longest_Common_Subsequence.c), [C++](https://github.com/Kumar-laxmi/Algorithms/blob/main/C%2B%2B/Dynamic-Programming/LongestCommonSubsequence.cpp), [Java](https://github.com/Kumar-laxmi/Algorithms/blob/main/Java/Dynamic-Programming/LongestCommonSubsequence.java), [Python](https://github.com/Kumar-laxmi/Algorithms/blob/main/Python/Dynamic-Programming/Longest_Common_Subsequence.py) |