4848#the cumulated steps we have taken should be matrix[m-1][n]+1
4949#for replacement, it is a lil bit tricky
5050#there are two scenarios
51- #if len(a)==len(b)
51+ #if a[m]==b[n]
5252#it should be matrix[m-1][n-1]
53- #we are replacing mth letter of string a with nth letter of string b
54- #the process is simultaneous
53+ #we dont need any replacement at all
5554#else, it should be matrix[m-1][n-1]+1
55+ #we replace mth letter of string a with nth letter of string b
5656#after we managed to understand three different approaches
5757#we want to take the minimum steps among these three approaches
5858#throughout the iteration of different positions of both strings
@@ -61,16 +61,23 @@ def edit(a,b):
6161 len_a = len (a )
6262 len_b = len (b )
6363
64+ #this part is to create a matrix of len(a)*len(b)
65+ #as lists start at index 0
66+ #we get a matrix of (len(a)+1)*(len(b)+1) instead
6467 c = []
6568 for i in range (len_a + 1 ):
6669 c .append ([0 ]* (len_b + 1 ))
67-
6870 for j in range (len_a + 1 ):
6971 c [j ][0 ]= j
70-
7172 for k in range (len_b + 1 ):
7273 c [0 ][k ]= k
73-
74+
75+ #we take iterations on both string a and b
76+ #next, we check if a[m]==b[n]
77+ #if yes, no replacement needed
78+ #if no, replacement needed
79+ #we take a minimum functions to see which combinations would give the minimum steps
80+ #eventually we got what we are after
7481 for l in range (1 ,len_a + 1 ):
7582 for m in range (len_b + 1 ):
7683 if a [l - 1 ]== b [m - 1 ]:
@@ -80,10 +87,9 @@ def edit(a,b):
8087
8188 return c [len_a ][len_b ]
8289
90+ #lets get some random strings to test
8391import random as rd
84-
85-
86-
92+
8793temp = rd .randint (1 ,20 )
8894temp1 = rd .randint (1 ,20 )
8995alphabet = 'abcdefghijklmnopqrstuvwxyz'
0 commit comments