Step 1: Initialize matrix with sizes (len(word1)+1) x (len(word2)+1) and fill first row and column with index values
'' c a r
'' 0 1 2 3
c 1 _ _ _
a 2 _ _ _
t 3 _ _ _
Why: We start with empty strings and count how many edits to get from empty to prefixes
Step 2: Compare 'c' (word1[0]) and 'c' (word2[0]), they match, so cost is 0; set matrix[1][1] = matrix[0][0] = 0
'' c a r
'' 0 1 2 3
c 1 0 _ _
a 2 _ _ _
t 3 _ _ _
Why: No change needed when letters match
Step 3: Compare 'c' and 'a', they differ; calculate min of insert(1+matrix[1][1]), delete(1+matrix[0][2]), replace(1+matrix[0][1]) = min(1+0,1+2,1+1)=1; set matrix[1][2] = 1
'' c a r
'' 0 1 2 3
c 1 0 1 _
a 2 _ _ _
t 3 _ _ _
Why: Choose cheapest edit operation
Step 4: Compare 'c' and 'r', differ; min(insert, delete, replace) = min(1+1,1+3,1+2)=2; set matrix[1][3] = 2
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 _ _ _
t 3 _ _ _
Why: Continue filling first row
Step 5: Compare 'a' and 'c', differ; min(insert, delete, replace) = min(1+1,1+0,1+1)=1; set matrix[2][1] = 1
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 1 _ _
t 3 _ _ _
Why: Fill second row first column
Step 6: Compare 'a' and 'a', match; set matrix[2][2] = matrix[1][1] = 0
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 1 0 _
t 3 _ _ _
Why: No edit needed for matching letters
Step 7: Compare 'a' and 'r', differ; min(insert, delete, replace) = min(1+0,1+2,1+1)=1; set matrix[2][3] = 1
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 1 0 1
t 3 _ _ _
Why: Fill second row last column
Step 8: Compare 't' and 'c', differ; min(insert, delete, replace) = min(1+2,1+2,1+1)=2; set matrix[3][1] = 2
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 1 0 1
t 3 2 _ _
Why: Fill third row first column
Step 9: Compare 't' and 'a', differ; min(insert, delete, replace) = min(1+0,1+2,1+1)=1; set matrix[3][2] = 1
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 1 0 1
t 3 2 1 _
Why: Fill third row second column
Step 10: Compare 't' and 'r', differ; min(insert, delete, replace) = min(1+1,1+1,1+0)=1; set matrix[3][3] = 1
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 1 0 1
t 3 2 1 1
Why: Fill last cell with minimum edits
Result: Final matrix:
'' c a r
'' 0 1 2 3
c 1 0 1 2
a 2 1 0 1
t 3 2 1 1
Minimum edit distance = 1