#K10246. Minimum Adjacent Swaps to Transform Strings

    ID: 23204 Type: Default 1000ms 256MiB

Minimum Adjacent Swaps to Transform Strings

Minimum Adjacent Swaps to Transform Strings

You are given two strings A and B of equal length. Your task is to transform string A into string B using only adjacent swaps. In each swap, you are allowed to swap the characters at positions i and i+1 for some valid index i. The cost of each swap is 1, and you must determine the minimum number of swaps required to make A equal to B. If it is impossible to transform A into B because they have different character multiset, output -1.

Mathematical Formulation:

If \(A\) cannot be rearranged to \(B\), then the answer is \(-1\). Otherwise, let \(n\) be the length of the strings and the answer is the minimum number of adjacent swaps required to align string A to B.

Example: For A = "abac" and B = "baca", the minimum number of swaps required is 2.

inputFormat

The input consists of two lines. The first line contains the string A and the second line contains the string B.

Constraints: Both strings have the same length. They consist of lowercase English letters.

outputFormat

Output a single integer representing the minimum number of adjacent swaps required to transform string A into string B. If it is impossible, output -1.

## sample
abac
baca
2