#C10552. Minimum Number of Operations to Transform Strings

    ID: 39770 Type: Default 1000ms 256MiB

Minimum Number of Operations to Transform Strings

Minimum Number of Operations to Transform Strings

You are given two strings \(s\) and \(t\). Your task is to determine the minimum number of adjacent swap operations required to transform string \(s\) into string \(t\).

The only allowed operation is:

  • Swap any two adjacent characters in \(s\).

The transformation is possible if and only if \(s\) and \(t\) have the same multiset of characters. If they do not, output \(-1\).

Note: Although the original problem statement mentioned additional operations (appending and removing characters), the intended solution only uses adjacent swaps for strings with equal character counts.

inputFormat

The input begins with an integer (q) representing the number of test cases. Each test case consists of two lines: the first line contains the string (s) and the second line contains the target string (t).

outputFormat

For each test case, output a single integer on a new line indicating the minimum number of adjacent swap operations required to transform (s) into (t). If the transformation is impossible, output (-1).## sample

3
ab
ba
abcd
dcba
a
a
1

6 0

</p>