Beats 97% || Explained in Detail || [Java/C++/Python/JavaScript]
Approach\n1. Create a Stack<Character> named st to store characters.\n2. Iterate through each character of the input string s using a loop.\n3. Check if the sta
# Approach\n1. Create a `Stack<Character>` named `st` to store characters.\n2. Iterate through each character of the input string s using a loop.\n3. Check if the stack is not empty.\n    - If not empty, compare the top element of the stack (`st.peek()`) with the current character (`s.charAt(i)`).\n        - If they are equal, pop the top element from the stack, effectively removing the adjacent duplicate.\n        - If they are not equal, push the current character onto the stack.\n    - If the stack is empty, push the current character onto the stack.\n4. After the loop, create a `StringBuilder` named sb to build the `result` string.\n5. Pop characters from the stack and append them to the `StringBuilder` until the stack is empty.\n6. Reverse the `StringBuilder` and convert it to a string using `toString()`.\n7. Return the final result.\n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\nclass Solution {\n    public String removeDuplicates(String s) {\n        // Create a stack to store characters.\n        Stack<Character> st = new Stack<>();\n\n        // Iterate through each character of the input string.\n        for (int i = 0; i < s.length(); i++) {\n            // Check if the stack is not empty.\n            if (!st.isEmpty()) {\n                // If the current character is equal to the top of the stack, remove the duplicate.\n                if (st.peek() == s.charAt(i)) {\n                    st.pop();\n                } else {\n                    // Push the current character onto the stack.\n                    st.push(s.charAt(i));\n                }\n            } else {\n                // If the stack is empty, push the current character onto the stack.\n                st.push(s.charAt(i));\n            }\n        }\n\n        // Build the result \n        StringBuilder sb = new StringBuilder();\n        // Pop characters from the stack and append them to the StringBuilder.\n        while (!st.isEmpty()) {\n            sb.append(st.pop());\n        }\n\n        // Reverse the StringBuilder and convert it to a string.\n        return sb.reverse().toString();\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    string removeDuplicates(string s) {\n        // Create a stack to store characters.\n        stack<char> st;\n\n        // Iterate through each character of the input string.\n        for (int i = 0; i < s.length(); i++) {\n            // Check if the stack is not empty.\n            if (!st.empty()) {\n                // If the current character is equal to the top of the stack, remove the duplicate.\n                if (st.top() == s[i]) {\n                    st.pop();\n                } else {\n                    // Push the current character onto the stack.\n                    st.push(s[i]);\n                }\n            } else {\n                // If the stack is empty, push the current character onto the stack.\n                st.push(s[i]);\n            }\n        }\n\n        // Build the result string.\n        string result;\n        // Pop characters from the stack and append them to the result string.\n        while (!st.empty()) {\n            result += st.top();\n            st.pop();\n        }\n\n        // Reverse the result string.\n        reverse(result.begin(), result.end());\n\n        return result;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def removeDuplicates(self, s: str) -> str:\n        # Create a stack to store characters.\n        st = []\n\n        # Iterate through each character of the input string.\n        for char in s:\n            # Check if the stack is not empty.\n            if st:\n                # If the current character is equal to the top of the stack, remove the duplicate.\n                if st[-1] == char:\n                    st.pop()\n                else:\n                    # Push the current character onto the stack.\n                    st.append(char)\n            else:\n                # If the stack is empty, push the current character onto the stack.\n                st.append(char)\n\n        # Build the result string by joining characters from the stack.\n        result = \'\'.join(st)\n\n        return result\n```\n```JavaScript []\n/**\n * @param {string} s\n * @return {string}\n */\nvar removeDuplicates = function(s) {\n    // Create an array to simulate a stack.\n    const stack = [];\n\n    // Iterate through each character of the input string.\n    for (const char of s) {\n        // Check if the stack is not empty.\n        if (stack.length > 0) {\n            // If the current character is equal to the top of the stack, remove the duplicate.\n            if (stack[stack.length - 1] === char) {\n                stack.pop();\n            } else {\n                // Push the current character onto the stack.\n                stack.push(char);\n            }\n        } else {\n            // If the stack is empty, push the current character onto the stack.\n            stack.push(char);\n        }\n    }\n\n    // Build the result string by joining characters from the stack.\n    const result = stack.join(\'\');\n\n    return result;\n};\n```
['Stack', 'C++', 'Java', 'Python3', 'JavaScript']