#P3531. [POI 2012] LIT-Letters

[POI 2012] LIT-Letters

Description

Given two strings aa, bb of equal length, each consisting only of uppercase letters, you may swap two adjacent characters in aa at a time. Find the minimum number of swaps needed so that the string obtained from aa becomes identical to bb.

Input Format

The first line contains an integer nn, the length of the strings.

The second line contains a string of length nn, denoting aa.

The third line contains a string of length nn, denoting bb.

Output Format

Output a single integer on one line, the minimum number of swaps.

3
ABC
BCA

2

Hint

Constraints

  • For 30% of the testdata, it is guaranteed that n103n \leq 10^3.
  • For 100% of the testdata, 1n1061 \leq n \leq 10^6, aa, bb contain only uppercase letters, and the testdata guarantees that aa can be transformed into bb.

Translated by ChatGPT 5