g2901_3000.s2957_remove_adjacent_almost_equal_characters.readme.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
2957\. Remove Adjacent Almost-Equal Characters
Medium
You are given a **0-indexed** string `word`.
In one operation, you can pick any index `i` of `word` and change `word[i]` to any lowercase English letter.
Return _the **minimum** number of operations needed to remove all adjacent **almost-equal** characters from_ `word`.
Two characters `a` and `b` are **almost-equal** if `a == b` or `a` and `b` are adjacent in the alphabet.
**Example 1:**
**Input:** word = "aaaaa"
**Output:** 2
**Explanation:** We can change word into "a**c**a**c**a" 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 "**y**bd**o**ez" 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 "z**a**x**a**x**a**z" 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.
**Constraints:**
* `1 <= word.length <= 100`
* `word` consists only of lowercase English letters.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy