#C4701. Minimum Operations to Convert a String

    ID: 48269 Type: Default 1000ms 256MiB

Minimum Operations to Convert a String

Minimum Operations to Convert a String

We are given an integer ( n ) and two strings ( s ) and ( t ), each of length ( n ). The task is to determine the minimum number of swap operations required to convert ( s ) into ( t ). In each operation, you can swap the characters at two different positions in ( s ). Note that each swap can fix two mismatched positions. However, if it is impossible to convert ( s ) to ( t ) (i.e. if the sorted characters of ( s ) and ( t ) differ), then the answer should be ( -1 ).

For instance, converting "abcd" to "dcba" requires 2 swap operations because swapping the first and last characters and then the second and third characters will achieve the result.

inputFormat

Input is read from standard input (stdin) and consists of three lines. The first line contains the integer ( n ). The second line contains the string ( s ). The third line contains the string ( t ).

outputFormat

Output the minimum number of swap operations required to convert ( s ) into ( t ), or output ( -1 ) if the conversion is impossible. The output should be written to standard output (stdout).## sample

4
abcd
dcba
2

</p>