Please consider the following question:
The edit distance of two strings s
and t
is the minimum number of single character operations (insert, delete, substitution) needed to convert s
into t
. Let m
and n
be the length of strings s
and t
.
Design an O(nm)
time and O(nm)
space algorithm to calculate the edit distance between s
and t
.
My thoughts:
Isn't it easier to just compare two strings one character at a time:
L = maximum(length(s), length(t))
for i in L:
if i > length(s):
distance += length(t) - i
break
if i > length(t):
distance += length(s) - i
break
if s[i] != t[i]:
distance += 1
If I am wrong, then am I supposed to use the edit distance algorithm table? Is so, how do I design an O(nm)
time and O(nm)
space algorithm?
Thank you @Roberto Attias for his answer, but the following is the complete algorithm I am looking for:
The above algorithm follows the strategy of an edit distance algorithm table