⟨⊇⊆⟩jg;?clᵐ-ˡṅ
Try it online!
This question has a surprising lack of golfing language solutions (there's an APL solution, which is a quasi-golfing language, but it doesn't beat the builtin-based solutions). So here's an attempt to get the ball rolling on that.
I'm not really happy with the golfiness of this; there's a lot of plumbing, as is so often the case when a Brachylog answer takes multiple inputs. In order to comprehensively beat the builtin-based solutions, ideally someone would come up with an answer that's shorter than the word "Levenshtein". Anyone feel like giving that a go?
Explanation
⟨⊇⊆⟩jg;?clᵐ-ˡṅ
⟨ ⟩ Find a value that is related to the first and second inputs:
⊇ {the first input} is a non-continguous superstring of it
⊆ it is a non-contiguous substring of {the second input}
j Concatenate that value to itself
g Form a singleton list containing that value
;?c Append the inputs to that list
lᵐ Take the length of each of the three elements
-ˡ Left fold by subtraction
ṅ Multiply the result by -1
The real work is done by the ⟨⊇⊆⟩
at the start, which finds the longest common subsequence of the two inputs (as specified, it just asks for any subsequence, but Brachlog's default tiebreaks will always pick the longest). Let's call the length of this q, and the lengths of the inputs a and b. The edit distance is then equal to a-q+b-q; to edit one into the other, we need to delete from one until we reach the common subsequence, then insert until we reach the other (when editing with insertions and deletions, the order doesn't matter).
After our ⟨⊇⊆⟩
, the current implicit variable is a string of length q. Doubling this with j
gives us a string of length 2q. After bringing the inputs back into our working value, we have three strings, of lengths [2q, a, b], and can replace these with their lengths. Reducing by subtraction then gives 2q-a-b = -(a-q+b-q), so we simply need to negate the answer to get the edit distance.
levenshtein
function treats substitutions as one edit (substitute), not two (delete + insert). \$\endgroup\$