Created
September 20, 2018 02:23
-
-
Save vamdt/36ee768bd5f123aeadca7b51ea39a891 to your computer and use it in GitHub Desktop.
Dynamic programming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
result = edit_distance("fox", "fab") | |
print(result) | |
def edit_distance(str1, str2): | |
len1 = len(str1) | |
len2 = len(str2) | |
maxtrix = [[i for j in range(len2 +1)] for i in range(len1 + 1)] | |
maxtrix[0] = [i for i in range(len1 + 1)] | |
for i in range(1, len1): | |
for j in range(1, len2): | |
if str1[i-1] == str2[j-1]: | |
maxtrix[i][j] = maxtrix[i-1][j-1] | |
else: | |
maxtrix[i][j] = min(maxtrix[i][j-1] + 1, maxtrix[i-1][j] + 1, maxtrix[i-1][j-1] + 1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment