#C4375. Taco Anagram Transformation
Taco Anagram Transformation
Taco Anagram Transformation
Given two strings S and T of equal length, your task is to determine the minimum number of operations required to transform the strings so that they become anagrams of each other. In one operation, you can swap two characters in S (or equivalently, in T).
Note that if S and T are not anagrams (i.e. they do not have the exact same frequency of characters), then it is impossible to make them anagrams by swapping, and you should output -1
.
The transformation works as follows: if we compare the two strings position by position, let
$$\text{mismatches} = \sum_{i=1}^{n} \mathbf{1}_{\{S_i \neq T_i\}},$$
then the minimum number of swap operations needed is given by
$$\frac{\text{mismatches}}{2},$$
provided that the mismatch count is even (which is guaranteed if S and T are anagrams). Otherwise, output -1
.
Implement a program that processes multiple test cases. For each case, read in the two strings and output the minimum number of operations required (or -1
if impossible).
inputFormat
The input is given via standard input (stdin) and has the following format:
Q S1 T1 S2 T2 ... SQ TQ
Here, Q
(an integer) denotes the number of test cases. For each test case, two strings S
and T
are provided on separate lines. It is guaranteed that the two strings have equal length.
outputFormat
For each test case, output a single line containing the minimum number of operations required to transform the string into an anagram of the other. If it is not possible, output -1
.
3
abcd
bcda
abcd
abcf
abc
acb
2
-1
1
</p>