2957. Remove Adjacent Almost-Equal Characters LeetCode Solution
In this guide, you will get 2957. Remove Adjacent Almost-Equal Characters LeetCode Solution with the best time and space complexity. The solution to Remove Adjacent Almost-Equal Characters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Remove Adjacent Almost-Equal Characters solution in C++
- Remove Adjacent Almost-Equal Characters solution in Java
- Remove Adjacent Almost-Equal Characters solution in Python
- Additional Resources

Problem Statement of Remove Adjacent Almost-Equal Characters
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 “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.
Constraints:
1 <= word.length <= 100
word consists only of lowercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2957. Remove Adjacent Almost-Equal Characters LeetCode Solution in C++
class Solution {
public:
int removeAlmostEqualCharacters(string word) {
int ans = 0;
int i = 1;
while (i < word.length())
if (abs(word[i] - word[i - 1]) <= 1) {
++ans;
i += 2;
} else {
i += 1;
}
return ans;
}
};
/* code provided by PROGIEZ */
2957. Remove Adjacent Almost-Equal Characters LeetCode Solution in Java
class Solution {
public int removeAlmostEqualCharacters(String word) {
int ans = 0;
int i = 1;
while (i < word.length())
if (Math.abs(word.charAt(i) - word.charAt(i - 1)) <= 1) {
++ans;
i += 2;
} else {
i += 1;
}
return ans;
}
}
// code provided by PROGIEZ
2957. Remove Adjacent Almost-Equal Characters LeetCode Solution in Python
class Solution:
def removeAlmostEqualCharacters(self, word: str) -> int:
ans = 0
i = 1
while i < len(word):
if abs(ord(word[i]) - ord(word[i - 1])) <= 1:
ans += 1
i += 2
else:
i += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.