Problem · String
Minimum Adjacent Transpositions
Learn this problemProblem statement
source and target contain the same ASCII characters with the same frequencies.
In one operation, swap two adjacent characters in source. Return the minimum number of operations required to transform source into target.
Function
minimumAdjacentSwaps(source: String, target: String) → longExamples
Example 1
source = "GUM"target = "MUG"return = 3Moving M to the front takes two swaps, then moving U ahead of G takes one more.
Example 2
source = "aabb"target = "bbaa"return = 4Each of the two b characters crosses both a characters.
Constraints
1 <= source.length == target.length <= 200000- Both strings contain the same ASCII characters with the same frequencies.