#P6758. Efficient Removal of 'e' in Vim

    ID: 19966 Type: Default 1000ms 256MiB

Efficient Removal of 'e' in Vim

Efficient Removal of 'e' in Vim

Victor has a string (S) of length (N). His goal is to remove all occurrences of the letter (e) from (S) while preserving all other characters. The catch is that he is using a simplified version of Vim with only three commands available:

  • x: Delete the character under the cursor. The cursor does not move after deletion. This command cannot be used if the cursor is at the last character of the string.
  • h: Move the cursor one position to the left. If the cursor is at the first character, it stays there.
  • f c: (where \(c \neq e\)) Move the cursor to the first occurrence to the right of the current position that equals the character \(c\).

Initially, the cursor is at the first character. It is guaranteed that the last character of \(S\) is not an \(e\) (so that every \(e\) can be deleted using the available commands).

The task is to determine the minimum number of keystrokes needed to delete all copies of \(e\) (using only these commands) while leaving every other character intact.

Note: All command keystrokes count individually. For example, the command f c counts as 2 keystrokes (one for f and one for the character \(c\)).
Explanation of an Optimal Strategy:
Observe that the only deletion command is x which can only be used if the cursor is not at the last character. This means that any block of consecutive \(e\)'s at the beginning (starting at index 0) can be deleted one by one with one keystroke each. For any block of consecutive \(e\)'s that does not start at the beginning, in order to reach the block, Victor must use the f command to jump to the first non-\(e\) character immediately after the block and then use h to move left into the block. Then he deletes the rightmost \(e\) in the block with x, and for each additional \(e</code> in that block he must move left one more time (with h) before deleting it. Hence, if a block (of length \(k\)) is not at the beginning, its total cost is \(2k+2\) keystrokes (2 for each \(e\) plus an extra 2 for positioning).

Thus, the overall minimum keystroke count equals the sum of keystrokes needed for each block of \(e\)'s. If the block is at the beginning, the cost is simply its length; otherwise, for a block of length \(k\) the cost is \(2k+2\).

inputFormat

The first line contains an integer (N) ((2 \le N \le 10^5)), the length of the string (S).

The second line contains the string (S) consisting of lowercase English letters. It is guaranteed that the last character of (S) is not (e).

outputFormat

Output a single integer representing the minimum number of keystrokes required to remove all occurrences of (e) from (S) using the available Vim commands.

sample

5
aeeaa
6