Easy to Understand Neat and Clean Solution in C++ || JAVA🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to remove almost-equal characters from the given word. Characters are consi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to remove almost-equal characters from the given word. Characters are considered almost equal if their absolute difference is at most 1 in terms of ASCII values. We want to count the number of removals needed to make the word free of almost-equal characters.\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIterate through the characters of the word starting from the second character (i = 1). For each pair of adjacent characters (word[i] and word[i - 1]), check if their absolute difference in ASCII values is at most 1. If true, increment the count of removals (count) and modify the current character (word[i]) to either \'a\' or \'z\' based on the next character (word[i + 1]).\n\n---\n\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n---\n\n\n# **Please Upvote \uD83D\uDE4F\uD83C\uDFFB**\n\n---\n\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int removeAlmostEqualCharacters(std::string word) {\n        int count = 0;\n        for (int i = 1; i < word.size(); i++) {\n            if (abs(word[i] - word[i - 1]) <= 1) {\n                count++;\n                word[i] = (word[i + 1] != \'a\' && word[i + 1] != \'b\') ? \'a\' : \'z\';\n            }\n        }\n        return count;\n    }\n};\n```\n```java []\npublic class Solution {\n    public int removeAlmostEqualCharacters(String word) {\n        int count = 0;\n        char[] wordArray = word.toCharArray();\n\n        for (int i = 1; i < wordArray.length; i++) {\n            if (Math.abs(wordArray[i] - wordArray[i - 1]) <= 1) {\n                count++;\n                wordArray[i] = (i + 1 < wordArray.length && (wordArray[i + 1] != \'a\' && wordArray[i + 1] != \'b\')) ? \'a\' : \'z\';\n            }\n        }\n\n        return count;\n    }\n}\n\n\n```
['C++', 'Java']