Papple Sort

AtCoder
IDarc088_c
Time2000ms
Memory256MB
Difficulty
You are given a string $S$ consisting of lowercase English letters. Determine whether we can turn $S$ into a palindrome by repeating the operation of swapping two adjacent characters. If it is possible, find the minimum required number of operations. ## Constraints * $1 \leq |S| \leq 2 × 10^5$ * $S$ consists of lowercase English letters. ## Input Input is given from Standard Input in the following format: $S$ [samples]
Samples
Input #1
eel
Output #1
1

We can turn $S$ into a palindrome by the following operation:

*   Swap the $2$\-nd and $3$\-rd characters. $S$ is now `ele`.
Input #2
ataatmma
Output #2
4

We can turn $S$ into a palindrome by the following operation:

*   Swap the $5$\-th and $6$\-th characters. $S$ is now `ataamtma`.
*   Swap the $4$\-th and $5$\-th characters. $S$ is now `atamatma`.
*   Swap the $3$\-rd and $4$\-th characters. $S$ is now `atmaatma`.
*   Swap the $2$\-nd and $3$\-rd characters. $S$ is now `amtaatma`.
Input #3
snuke
Output #3
\-1

We cannot turn $S$ into a palindrome.
API Response (JSON)
{
  "problem": {
    "name": "Papple Sort",
    "description": {
      "content": "You are given a string $S$ consisting of lowercase English letters. Determine whether we can turn $S$ into a palindrome by repeating the operation of swapping two adjacent characters. If it is possibl",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc088_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a string $S$ consisting of lowercase English letters. Determine whether we can turn $S$ into a palindrome by repeating the operation of swapping two adjacent characters. If it is possibl...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments