Remove Adjacent
Contest: Codeforces Round 625 (Div. 2, based on Technocup 2020 Final Round)
Difficulty rating: 1600
Tags: brute force, constructive algorithms, greedy, strings

You are given a string $$$s$$$ consisting of lowercase Latin letters. Let the length of $$$s$$$ be $$$|s|$$$. You may perform several operations on this string.

In one operation, you can choose some index $$$i$$$ and remove the $$$i$$$-th character of $$$s$$$ ($$$s_i$$$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $$$s_i$$$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $$$i$$$ should satisfy the condition $$$1 \le i \le |s|$$$ during each operation.

For the character $$$s_i$$$ adjacent characters are $$$s_{i-1}$$$ and $$$s_{i+1}$$$. The first and the last characters of $$$s$$$ both have only one adjacent character (unless $$$|s| = 1$$$).

Consider the following example. Let $$$s=$$$ bacabcab.

1. During the first move, you can remove the first character $$$s_1=$$$ b because $$$s_2=$$$ a. Then the string becomes $$$s=$$$ acabcab.
2. During the second move, you can remove the fifth character $$$s_5=$$$ c because $$$s_4=$$$ b. Then the string becomes $$$s=$$$ acabab.
3. During the third move, you can remove the sixth character $$$s_6=$$$'b' because $$$s_5=$$$ a. Then the string becomes $$$s=$$$ acaba.
4. During the fourth move, the only character you can remove is $$$s_4=$$$ b, because $$$s_3=$$$ a (or $$$s_5=$$$ a). The string becomes $$$s=$$$ acaa and you cannot do anything with it.

Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.

Input format:
The first line of the input contains one integer $$$|s|$$$ ($$$1 \le |s| \le 100$$$) — the length of $$$s$$$.

The second line of the input contains one string $$$s$$$ consisting of $$$|s|$$$ lowercase Latin letters.

Output format:
Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $$$4$$$.

In the second example, you can remove all but one character of $$$s$$$. The only possible answer follows.

1. During the first move, remove the third character $$$s_3=$$$ d, $$$s$$$ becomes bca.
2. During the second move, remove the second character $$$s_2=$$$ c, $$$s$$$ becomes ba.
3. And during the third move, remove the first character $$$s_1=$$$ b, $$$s$$$ becomes a.

Example input:
8
bacabcab

Example output:
4

Example input:
4
bcda

Example output:
3

Editorial:
The optimal answer can be obtained by the following algorithm: choose the maximum possible (alphabetically) letter we can remove and remove it. We can do it naively and it will lead to $$$O(n^2)$$$ time complexity.
Why is it always true? Suppose we remove the maximum letter $$$i$$$ that can be used to remove some other letter $$$j$$$ (it is obvious that $$$s_i + 1 = s_j$$$ in such case).
If there are no other letters between $$$s_i$$$ and $$$s_j$$$ then $$$s_i$$$ is not the maximum letter, so we got contradiction with our algorithm.
Now suppose that we can remove all letters between $$$s_i$$$ and $$$s_j$$$. Then we first choose $$$s_j$$$ and only after that $$$s_i$$$ by our algorithm.
Consider the last case — there is at least one letter $$$s_k$$$ between $$$s_i$$$ and $$$s_j$$$. Because we cannot remove $$$s_k$$$ then there are only two cases: $$$s_k > s_j + 1$$$ or $$$s_k + 1 < s_i$$$. Then we cannot use $$$s_i$$$ to remove $$$s_j$$$ at all.