#C8380. Minimum Adjacent Swaps to Equalize Binary Strings
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"
andb = "1001"
, the answer is2
. - If
a = "10101"
andb = "10110"
, the answer is1
. - If
a = "111"
andb = "111"
, the answer is0
. - If the number of
'1'
characters differ (e.g.a = "1100"
andb = "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 asa
).
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.
6
1100
1001
10101
10110
111
111
1100
1110
100
000
10
01
2
1
0
-1
-1
1
</p>