Beats 100%
Intuition\nTo remove adjacent almost-equal characters from the string, you need to perform the minimum number of operations. An operation involves replacing a c
### Intuition\nTo remove adjacent almost-equal characters from the string, you need to perform the minimum number of operations. An operation involves replacing a character to eliminate adjacent pairs of almost-equal characters. Characters are almost-equal if they are either the same or adjacent in the alphabet.\n\n### Approach\n1. **Use a Stack:**  \n   - Traverse the string character by character.\n   - Use a stack to keep track of characters that haven\'t been removed.\n   - For each character in the string:\n     - If the stack is empty, push the character onto the stack.\n     - If the stack is not empty, check the top of the stack:\n       - If the top character is almost-equal to the current character (i.e., either the same or adjacent in the alphabet), it means a removal is needed.\n       - Push a placeholder value (\'1\') to indicate that a removal has occurred, increment the count of operations, and push the current character onto the stack.\n       - If not almost-equal, push the current character onto the stack.\n   \n2. **Count the Operations:**  \n   - Every time a pair of almost-equal characters is removed, increment the operation count.\n\n### Complexity\n- **Time complexity:**  \n  The solution involves a single traversal of the string and stack operations, which are O(1) on average. Therefore, the time complexity is **O(n)**, where `n` is the length of the string.\n\n- **Space complexity:**  \n  The space complexity is **O(n)**, as in the worst case, all characters are pushed onto the stack.\n\n### Code\n```cpp\n#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n    int removeAlmostEqualCharacters(string word) {\n        stack<char> st;\n        int ans = 0;\n        for (auto i : word) {\n            if (st.empty()) {\n                st.push(i);\n            } \n            else {\n                if (st.top() == i || st.top() == i - 1 || st.top() == i + 1) {\n                    st.push(\'1\');\n                    ans++;\n                } \n                else {\n                    st.push(i);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']