Yet Another AB Problem

AtCoder
IDarc170_a
Time2000ms
Memory256MB
Difficulty
You are given two strings $S$ and $T$ of length $N$ consisting of `A` and `B`. Let $S_i$ denote the $i$\-th character from the left of $S$. You can repeat the following operation any number of times, possibly zero: * Choose integers $i$ and $j$ such that $1\leq i < j \leq N$. Replace $S_i$ with `A` and $S_j$ with `B`. Determine if it is possible to make $S$ equal $T$. If it is possible, find the minimum number of operations required. ## Constraints * $2 \leq N \leq 2 \times 10^5$ * Each of $S$ and $T$ is a string of length $N$ consisting of `A` and `B`. * All input numbers are integers. ## Input The input is given from Standard Input in the following format: $N$ $S$ $T$ [samples]
Samples
Input #1
5
BAABA
AABAB
Output #1
2

Performing the operation with $i=1$ and $j=3$ changes $S$ to `AABBA`.
Performing the operation with $i=4$ and $j=5$ changes $S$ to `AABAB`.
Thus, you can make $S$ equal $T$ with two operations. It can be proved that this is the minimum number of operations required, so the answer is $2$.
Input #2
2
AB
BA
Output #2
\-1

It can be proved that no matter how many operations you perform, you cannot make $S$ equal $T$.
API Response (JSON)
{
  "problem": {
    "name": "Yet Another AB Problem",
    "description": {
      "content": "You are given two strings $S$ and $T$ of length $N$ consisting of `A` and `B`. Let $S_i$ denote the $i$\\-th character from the left of $S$. You can repeat the following operation any number of times, ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc170_a"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given two strings $S$ and $T$ of length $N$ consisting of `A` and `B`. Let $S_i$ denote the $i$\\-th character from the left of $S$.\nYou can repeat the following operation any number of times, ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments