Skip to content

Commit

Permalink
Pushing Edit Distance algorithm in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
Kumar Laxmikant authored and Kumar Laxmikant committed Oct 3, 2024
1 parent 65271de commit 2c4f244
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Python/Dynamic-Programming/edit_distance.py
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down

0 comments on commit 2c4f244

Please sign in to comment.