#K13696. Minimum Swaps to Equal Strings

    ID: 23970 Type: Default 1000ms 256MiB

Minimum Swaps to Equal Strings

Minimum Swaps to Equal Strings

You are given two strings a and b of equal length n. Your task is to determine the minimum number of swaps required to make the two strings equal. In one swap, you can exchange the characters at any two positions in one of the strings.

It is known that if the sorted characters of a and b are not identical, then no amount of swaps can make the strings equal. Otherwise, the minimum number of swaps required is given by the formula:

\[ \text{result} = \frac{1}{2}\sum_{i=1}^{n} \mathbf{1}\{a_i \neq b_i\} \]

If it is impossible to make the strings equal, output \(-1\). Each test case should be solved independently.

inputFormat

The input begins with an integer t ((1 \le t \le 10^5)) denoting the number of test cases. Each test case consists of three lines. The first line contains a single integer n (the length of the strings). The second line contains the string a and the third line contains the string b.

outputFormat

For each test case, print a single line with the minimum number of swaps required to make a and b equal, or (-1) if it is impossible.## sample

5
5
ababa
baaba
4
abcd
dcba
6
abcabc
cbacba
3
aaa
aaa
3
abc
abd
1

2 2 0 -1

</p>