Remove Substrings

AtCoder
IDagc054_a
Time2000ms
Memory256MB
Difficulty
Given is a string $S$ of length $N$ consisting of lowercase English letters. You can do the following operation on $S$ any number of times. * Choose its (non-empty) contiguous substring such that the first character and the last character are different, and delete this substring. Determine whether it is possible to make $S$ empty. If it is possible, find the minimum number of operations needed to do so. ## Constraints * $2 \leq N \leq 10^5$ * $S$ is a string of length $N$ consisting of lowercase English letters. ## Input Input is given from Standard Input in the following format: $N$ $S$ [samples]
Samples
Input #1
4
abba
Output #1
2

We should do as follows: `abba` → (delete `ab`) → `ba` → (delete `ba`) → an empty string.
Input #2
3
aba
Output #2
\-1
API Response (JSON)
{
  "problem": {
    "name": "Remove Substrings",
    "description": {
      "content": "Given is a string $S$ of length $N$ consisting of lowercase English letters. You can do the following operation on $S$ any number of times. *   Choose its (non-empty) contiguous substring such that t",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc054_a"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given is a string $S$ of length $N$ consisting of lowercase English letters.\nYou can do the following operation on $S$ any number of times.\n\n*   Choose its (non-empty) contiguous substring such that t...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments