0

Starting from "abcd", I want to go to "badc" :

  • I remove "a" -> "bcd";
  • I insert "a" at the right position -> "bacd";
  • I remove "c" -> "bad";
  • I insert "c" at the right position -> "badc".

It's 4 operations. I can't find out a shorter way to do it. However, Levenshtein distance returns me a cost of 3. Why is that?

Thanks for your response.

jules
  • 73
  • 2
  • 8

1 Answers1

0

You can go from abcd to badc in 3 operations. Remember you can do insertions, deletions and substitutions:

  1. Remove a -> bcd.
  2. Substitute c with a -> bad.
  3. Add c at the end -> badc.

More generally, an insert + remove pair of operations can be coalesced into a substitution if the insertion and the removal are adjacent.

Banex
  • 2,890
  • 3
  • 28
  • 38
  • Pfiouu thank you, I'm stuck with this for an hour. Sometimes the simplest problems are the most difficult to solve, especially when the answer is just in front of you... – jules Aug 09 '18 at 10:29