O(n) sliding window
Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe first concern is that performing the operation on one character might accidentall
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe first concern is that performing the operation on one character might accidentally make it almost equal to another character, but this was quickly dismissed -- we just need to make the value different than both neighbors. \n\nThe other thought is that the answer is relatively clear if we have a string of length n where all the characters are almost equal to each other, the answer should be floor(n / 2), since we can change every other value, starting at the second character.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nWe use a sliding window to keep track of the current length of the substring where the ith character is almost equal to the (i+1)th character. Once we find a character that is not almost equal to the previous, we add floor(right - left / 2).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```\nclass Solution:\n    def removeAlmostEqualCharacters(self, word: str) -> int:\n        def almost_equal(a,b):\n            return abs(ord(a)-ord(b)) < 2\n        #if there is a chain of length n, need to change n // 2?\n        ans = 0\n        left = 0\n        prev = word[left]\n        for right in range(1,len(word)):\n            if not almost_equal(word[right], prev):\n                ans += (right - left) // 2\n                left = right\n            prev = word[right]\n        ans += (len(word) - left) // 2\n        return ans\n\n        \n```
['Python3']