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
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.
Python code for this problem given below : def editDistance(str1, str2, m, n): if m == 0: return n if n == 0: return m i
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am