AB Palindrome

AtCoder
IDarc145_a
Time2000ms
Memory256MB
Difficulty
You are given a string $S$ of length $N$ consisting of `A` and `B`. You can repeat the following operation zero or more times: * choose a pair of adjacent characters in $S$ and replace them with `AB`. Determine whether $S$ can be turned into a palindrome. What is a palindrome? A string $T$ is a palindrome if and only if, for every integer $i$ ($1 \le i \le |T|$), the $i$\-th character from the beginning and the $i$\-th character from the end are the same, where $|T|$ is the length of $T$. ## Constraints * $2 \leq N \leq 2\times 10^5$ * $S$ is a string of length $N$ consisting of `A` and `B`. ## Input Input is given from Standard Input in the following format: $N$ $S$ [samples]
Samples
Input #1
3
BBA
Output #1
Yes

Replacing the $2$\-nd and $3$\-rd characters, `BA`, with `AB` will turn $S$ into `BAB`, a palindrome.
Input #2
4
ABAB
Output #2
No

No sequence of operations can turn $S$ into a palindrome.
API Response (JSON)
{
  "problem": {
    "name": "AB Palindrome",
    "description": {
      "content": "You are given a string $S$ of length $N$ consisting of `A` and `B`. You can repeat the following operation zero or more times: *   choose a pair of adjacent characters in $S$ and replace them with `A",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc145_a"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a string $S$ of length $N$ consisting of `A` and `B`.\nYou can repeat the following operation zero or more times:\n\n*   choose a pair of adjacent characters in $S$ and replace them with `A...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments