Menu Close

Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.

Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.
1.Insert
2.Remove
3.Replace
All of the above operations are of equal cost.

def operations(s1,s2,l1,l2):
	if l1==0:
		return l2
	if l2==0:
		return l1

	if s1[l1-1]==s2[l2-1]:
		return operations(s1,s2,l1-1,l2-1)

	return 1 + min( operations(s1,s2,l1,l2-1) , operations(s1,s2,l1-1,l2) , operations(s1,s2,l1-1,l2-1) )

s1=input('Enter string Str1 : ')
s2=input('Enter string Str2 : ')

print(operations(s1,s2,len(s1),len(s2)))

Input_1:
Enter string Str1 : sunday
Enter string Str2 : saturday

Output:
3


Input_2:
Enter string Str1 : fax
Enter string Str2 : fox

Output:
1


Input_3:
Enter string Str1 : fuckthecode
Enter string Str2 : fcukthecode

Output:
2


Input_4:
Enter string Str1 : fuckthecoed
Enter string Str2 : fuckthecode

Output:
2


Input_5:
Enter string Str1 : python
Enter string Str2 : python3

Output:
1


Illustration of the Output : ?

Executed using terminal linux(python3)

More Q