13
4
Explanation
The edit distance between two strings is a function of the minimum possible number of insertions, deletions, or substitutions to convert one word into another word.
Insertions and deletions cost 1, and substitutions cost 2.
For example, the distance between AB
and A
is 1, because deletions cost 1 and the only edit needed is the deletion of the B
character.
The distance between CAR
and FAR
is 2, because substitutions cost 2. Another way to look at this is one deletion and one insertion.
Rules
Given two input strings (supplied however is convenient in your language), your program must find the minimum edit distance between the two strings.
You may assume that the strings only contain the characters A-Z
and have fewer than 100 characters and more than 0 characters.
This is code golf, so the shortest solution wins.
Sample Test Cases
ISLANDER, SLANDER
> 1
MART, KARMA
> 5
KITTEN, SITTING
> 5
INTENTION, EXECUTION
> 8
I did this in excel in my algorithms class once. Was kinda fun to turn the project in as an .xls document. It actually worked pretty well. I'll see if I can find it. – captncraig – 2012-03-16T17:32:54.153
PHP should win this easily. – st0le – 2012-03-19T12:13:58.900
@st0le - The built in
levenshtein
function treats substitutions as one edit (substitute), not two (delete + insert). – Mr. Llama – 2012-03-20T17:25:15.720