#C9347. Minimum Swaps to Make Strings Identical
Minimum Swaps to Make Strings Identical
Minimum Swaps to Make Strings Identical
You are given two strings s and t of length n. Your task is to determine the minimum number of swap operations required to make the two strings identical, by swapping characters in t. If it is impossible to make them identical, output -1
.
The problem can be formulated as follows:
Given two strings s and t each of length n, find the minimum number of swaps on t such that after all swaps, for every index i (where \(0 \le i < n\)), we have \(s_i = t_i\). If it is impossible (i.e. the multisets of characters in s and t are different), output \(-1\).
Note: A swap operation consists of choosing two indices in t and exchanging their characters.
Example:
Input: 4 abcd cdab</p>Output: 2
inputFormat
The input is read from stdin and consists of three lines:
- The first line contains an integer \(n\), the length of the strings.
- The second line contains the string s.
- The third line contains the string t.
outputFormat
The output is written to stdout and is a single integer representing the minimum number of swaps needed to make s and t identical. If it is impossible, output -1
.
4
abcd
cdab
2
</p>