Page 1 of 1

Python code for this problem given below : def editDistance(str1, str2, m, n): if m == 0: return n if n == 0: return m i

Posted: Sun Jul 10, 2022 11:29 am
by answerhappygod
Python code for this problem given below :
def editDistance(str1, str2, m, n):
if m == 0:return n
if n == 0:return m
if str1[m-1] == str2[n-1]:return editDistance(str1, str2, m-1, n-1)
return 1 + min(editDistance(str1, str2, m, n-1), # InserteditDistance(str1, str2, m-1, n), # RemoveeditDistance(str1, str2, m-1, n-1) # Replace)
str1 = str(input("Enter string 1 : "))str2 = str(input("Enter string 2 : "))print (editDistance(str1, str2, len(str1), len(str2)))
below is the code and its output. and please help the followingquestion
Python Code For This Problem Given Below Def Editdistance Str1 Str2 M N If M 0 Return N If N 0 Return M I 1
Python Code For This Problem Given Below Def Editdistance Str1 Str2 M N If M 0 Return N If N 0 Return M I 1 (141.41 KiB) Viewed 39 times
1. is the time and space complexity of this code?
When comparing two strings usually two characters at a time arecompared.If they are the same, the distance is the previous distance.•If a blank needs to be introduced add a 1 to the distance.•If a character needs to be deleted add a 1 to the distance.•If a substitution needs to be made add a 2 to the distance.Execute your code to compare the source AGCTAGTAAG and widththetarget GCAGTAGTATG.What is the distance between these two strings and what is thesequence ofedit operation
Code and Output: 1 def editDistance(str1, str2, m, n): 2 3- 4 5 6 7 8 9 10 11 12 13 14 15 16 TE LO if m == 0: return n if n == 0: return m if str1[m-1] == str2[n-1]: return editDistance(str1, str2, m-1, n-1) return 1 + min(editDistance(str1, str2, m, n-1), # Insert editDistance (str1, str2, m-1, n), # Remove editDistance (str1, str2, m-1, n-1) # Replace ) 17 18 str1 = str(input("Enter string 1: ")) 19 str2 = str(input("Enter string 2 : ")) 20 print (editDistance (str1, str2, len(str1), len(str2))) 21 Enter string 1 : jacob Enter string 2 : john 4 ... Program finished with exit code 0 Press ENTER to exit console.