diff --git a/algorithmic_toolbox/week_5/04_dynamic_programming_starter_files/edit_distance/edit_distance.py b/algorithmic_toolbox/week_5/04_dynamic_programming_starter_files/edit_distance/edit_distance.py index e259531..d7f70c0 100644 --- a/algorithmic_toolbox/week_5/04_dynamic_programming_starter_files/edit_distance/edit_distance.py +++ b/algorithmic_toolbox/week_5/04_dynamic_programming_starter_files/edit_distance/edit_distance.py @@ -1,7 +1,37 @@ -# Uses python3 -def edit_distance(s, t): - #write your code here - return 0 - +# A Dynamic Programming based Python program for edit +# distance problem +def editDistDP(str1, str2, m, n): + # Create a table to store results of subproblems + dp = [[0 for x in range(n + 1)] for x in range(m + 1)] + + # Fill d[][] in bottom up manner + for i in range(m + 1): + for j in range(n + 1): + + # If first string is empty, only option is to + # insert all characters of second string + if i == 0: + dp[i][j] = j # Min. operations = j + + # If second string is empty, only option is to + # remove all characters of second string + elif j == 0: + dp[i][j] = i # Min. operations = i + + # If last characters are same, ignore last char + # and recur for remaining string + elif str1[i-1] == str2[j-1]: + dp[i][j] = dp[i-1][j-1] + + # If last character are different, consider all + # possibilities and find minimum + else: + dp[i][j] = 1 + min(dp[i][j-1], # Insert + dp[i-1][j], # Remove + dp[i-1][j-1]) # Replace + + return dp[m][n] if __name__ == "__main__": - print(edit_distance(input(), input())) + str1=input() + str2=input() + print(editDistance(str1, str2,len(str1),len(str2)))