#C2792. Minimum Operations to Transform Strings

    ID: 46147 Type: Default 1000ms 256MiB

Minimum Operations to Transform Strings

Minimum Operations to Transform Strings

You are given two strings s and t. In one operation you can perform a transformation on s to try and obtain t. However, a transformation is only possible if s and t are anagrams, i.e. they have the same multiset of characters. Formally, let \( s \) and \( t \) be two strings. You need to determine the minimum number of operations required to transform \( s \) into \( t \) following these rules:

  • If \( s = t \), then no operation is needed (answer is 0).
  • If \( s \) and \( t \) are anagrams but not equal, then a single operation is sufficient (answer is 1). In particular, if the reverse of \( s \) equals \( t \), the answer is 1.
  • If \( s \) and \( t \) are not anagrams, then the transformation is impossible (answer is -1).

Note that the operation is abstract and you only need to compute the minimum number of required operations.

inputFormat

The input begins with an integer \( T \) (the number of test cases). Each test case is described by two lines, each containing a string. The first string represents \( s \) and the second string represents \( t \).

Example:

4
abcd
abcd
abcd
dcba
abba
baab
aaaa
bbbb

outputFormat

For each test case, print a single line containing the minimum number of operations required to transform \( s \) into \( t \). If the transformation is impossible, print -1.

Example:

0
1
1
-1
## sample
4
abcd
abcd
abcd
dcba
abba
baab
aaaa
bbbb
0

1 1 -1

</p>