#2957

Remove Adjacent Almost-Equal Characters

specialist · 830 · lc medium +31 · verified · 53.5% accepted · 206 likes · top 45%

Description

Given a 0-indexed string word, you can change any character to any lowercase English letter in one operation.

Two adjacent characters are almost-equal if they are identical or differ by exactly one position in the alphabet.

Return the minimum operations needed to eliminate all adjacent almost-equal pairs.

Example 1:

Input: word = "aaaaa"
Output: 2
Explanation: We can change word into "acaca" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.

Example 2:

Input: word = "abddez"
Output: 2
Explanation: We can change word into "ybdoez" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.

Example 3:

Input: word = "zyxyxyz"
Output: 3
Explanation: We can change word into "zaxaxaz" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.

Code

1
2
3