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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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 38 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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply