100%🚀+Video Solution🏆 | Simplest and Easiest Solution | O(n) | Fast | C++
Intuition\n Describe your first thoughts on how to solve this problem. \n- The goal is to minimize the number of operations to remove adjacent almost-equal char
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The goal is to minimize the number of operations to remove adjacent almost-equal characters.\n- Characters are almost-equal if they are the same or adjacent in the alphabet.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize variables: n as the length of the string, cnt to count consecutive almost-equal characters, and ans to store the minimum operations.\n2. Iterate through the string from index 1 to n-1.\n3. If the current character and the previous one are almost-equal, increment the cnt counter.\n4. If they are not almost-equal, update the ans by adding cnt/2 to it.\n Because, if we have a subarray of almost-equal elements of size \'m\', then we need change at least `floor(m/2)` characters.\nFor example, \n - - For subarray containing `a`, required changes = 0.\n - - For subarray containing `aa`, required changes = 1, i.e., `az`.\n - - For subarray containing `aaa`, required changes = 1, i.e., `aza`.\n - - For subarray containing `aaaa`, required changes = 2, i.e., `azaz`.\n - - For subarray containing `aaaaa`, required changes = 2, i.e. `azaza`.\n5. Reset cnt to 1 for the new sequence of characters.\n6. After the loop, update ans by adding the final cnt/2.\n7. Return the computed ans as the minimum number of operations.\n\nhttps://youtu.be/rO-odwo8Ecw\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# Code\n```\nclass Solution {\npublic:\n    int removeAlmostEqualCharacters(string w) {\n        int n = w.size();\n        int cnt = 1;\n        int ans=0;\n        for(int i=1; i<n; i++){\n            // Checking if the previous character is equal or adjacent, i.e. almost equal\n            if(w[i]==w[i-1] or w[i]+1==w[i-1] or w[i]-1==w[i-1]){\n                cnt++;\n            }\n            else{\n                ans += cnt/2;\n                cnt = 1;\n            }\n        }\n        ans += cnt/2;\n        return ans;\n    }\n};\n```
['Math', 'C++']