Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
First, ignore all the already matched positions, they don't affect the answer at all. For the unmatched positions, there are three basic cases (already given in the examples):
("xx", "yy") => 1 swap, ("xy", "yx") => 2 swaps
So the strategy is, apply case 1 as much as possible, then apply case 2 if the last two unmatched are in this case, or fall into impossible if only one pair of unmatched left. This can be done via a simple math.