Sliding window java solution
IntuitionUse sliding window to find length of each subarray which has adjacent
characters. Increment answer every time we find a length of a subarray
with adjac
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse sliding window to find length of each subarray which has adjacent\ncharacters. Increment answer every time we find a length of a subarray\nwith adjacent character by half the length since that is minimum\nnumber of modifications to make to ensure the word has no\nadjacent characters.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSliding window. Left pointer is at the start of \nsubarray with adjacent characters and right pointer is one character\nbeyond the end of the subarray so we can use right - left + 1 to calculate\nthe length. The last character in the word is a special case where\nwe take length / 2 instead (length - 1) / 2 since there will not\nbe another character.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```java []\nclass Solution {\n    public int removeAlmostEqualCharacters(String word) {\n        int left = 0;\n        int curLength = 1;\n        int ans = 0;\n        for(int right = 0; right < word.length(); ++right){                                    \n            curLength = right - left + 1;\n            if(adjacent(word, Math.max(0, right - 1), right)){\n                if(word.length() - 1 == right){\n                    ans += curLength / 2;\n                }                \n            }else{\n                ans += (curLength - 1) / 2;\n                left = right;\n            }\n        }\n        return ans;\n    }\n\n    boolean adjacent(String word, int left, int right){\n        char leftChar = word.charAt(left);\n        char rightChar = word.charAt(right);\n        return leftChar == rightChar || Math.abs((int)leftChar - (int)rightChar) == 1;\n    }\n}\n```
['Java']