#C8380. Minimum Adjacent Swaps to Equalize Binary Strings

    ID: 52356 Type: Default 1000ms 256MiB

Minimum Adjacent Swaps to Equalize Binary Strings

Minimum Adjacent Swaps to Equalize Binary Strings

Given two binary strings a and b of equal length, determine the minimum number of adjacent swaps required to transform string a into string b. In each swap, only two adjacent characters can be exchanged.

Note: The transformation is possible only if both strings have the same number of '1' characters.

For example:

  • If a = "1100" and b = "1001", the answer is 2.
  • If a = "10101" and b = "10110", the answer is 1.
  • If a = "111" and b = "111", the answer is 0.
  • If the number of '1' characters differ (e.g. a = "1100" and b = "1110"), output -1.

inputFormat

The input begins with an integer T (T ≥ 1), representing the number of test cases. Each test case consists of two lines:

  • The first line contains the binary string a.
  • The second line contains the binary string b (with the same length as a).

outputFormat

For each test case, output a single integer: the minimum number of adjacent swaps required to transform a into b. If the transformation is impossible, output -1. Each answer should be printed on its own line.

## sample
6
1100
1001
10101
10110
111
111
1100
1110
100
000
10
01
2

1 0 -1 -1 1

</p>