#C4899. Minimum Swap Transformation
Minimum Swap Transformation
Minimum Swap Transformation
Given two strings (S) and (T) of equal length, determine the minimum number of swaps required to transform (S) into (T). A swap is defined as an exchange of two characters in (S).
This problem can be solved by viewing the transformation as a permutation that can be decomposed into cycles. The minimum number of swaps needed is given by the formula: (\displaystyle \text{swaps} = \sum (\text{cycle_size} - 1)).
If (S) and (T) are not anagrams (i.e. their sorted order differs), it is impossible to transform (S) into (T), and the answer should be (-1).
Example: For (S = \text{"mango"}) and (T = \text{"gonam"}), the output is 3.
inputFormat
The input consists of two lines. The first line contains the string (S), and the second line contains the string (T). Both strings are guaranteed to have the same length.
outputFormat
Output a single integer representing the minimum number of swaps required to transform (S) into (T). If the transformation is impossible, output (-1).## sample
mango
gonam
3