Basically you ask the algorithm to change the distance , but only allow the transposition operation (aka swapping, twiddling). In the book Introduction to Algorithms, you will find hints for implementing the twiddle operation; this is one of the problems at the end of the chapter on dynamic programming. In addition, in the book "Guide to the Development of Algorithms" in the chapter on dynamic programming, there is a complete implementation of the distance editing algorithm in C - without the transpose operation (again, this is one of the proposed exercises at the end of the chapter).
In the above link, you will find that a typical way to implement a distance editing algorithm is to use dynamic programming, which has the cost of O (mn) and O (mn). As far as I know, there is no way to do this faster (for example, in less than O (mn) time), but you can do it in less space - being smart, you can reduce the space to O (m), given that to calculate the cost of the operation Transpose only the current row and the previous two rows in the table are needed.
That is, assuming you only need editing distance. If you need real editing operations, you are stuck using O (mn) space to restore a solution if you use dynamic programming. However, you can reduce the space to O (min {m, n}) and restore the actual editing operations using the Hirschberg algorithm .
source share