Given two anagrams S and P, what is the minimum edit distance from S to P when there are only two operations:
- swap two adjacent elements
- swap the first and the last element
If this question is simplified to only having the first operation (i.e. swap two adjacent elements) then this question is "similar to" the classical algorithm question of "the minimum number of swaps for sorting an array of numbers" (solution link is given below)
Sorting a sequence by swapping adjacent elements using minimum swaps
I mean "similar to" because when the two anagrams have all distinct characters:
S: A B C D
P : B C A D
Then we can define the ordering in P like this
P: B C A D
1 2 3 4
Then based on this ordering the string S becomes
S: A B C D
3 1 2 4
Then we can use the solution given in the link to solve this question.
However, I have two questions:
In the simplified question that we can only swap two adjacent elements, how can we get the minimum number of swaps if the anagrams contain duplicate elements. For example,
S: C D B C D A A
P: A A C D B C D
How to solve the complete question with two swap operations?