✅ beats 98.82% 🏆 optimal LINEAR solution
Intuition\nThe code appears to be aimed at removing consecutive duplicate characters from the input string s.\n Describe your first thoughts on how to solve thi
# Intuition\nThe code appears to be aimed at removing consecutive duplicate characters from the input string s.\n<!-- Describe your first thoughts on how to solve this problem. -->\n# Approach\nThe approach used is a simple iteration through the input string. For each character, it checks whether it is the same as the last character in the ans string. If it is, the last character in ans is popped off (removed), effectively eliminating the consecutive duplicate. If the character is different, it is appended to the ans string. The process continues until all characters in the input string are processed.\n\n# Complexity\nTime Complexity : O(n) - where n is the length of the input string s. The code iterates through the string once.\nSpace complexity: O(n) - where n is the length of the input string s. The space required is proportional to the length of the input string, as the ans string is used to store the result.\n\n# Code\n```\nclass Solution {\npublic:\n    string removeDuplicates(string s) {\n        string ans="";\n        for(int i =0;i<s.length();i++){\n            if(ans.length()>0){\n                if(ans[ans.length()-1]==s[i]){\n                    ans.pop_back();\n                }\n                else{\n                    ans.push_back(s[i]);\n                }\n            }\n            else{\n                ans.push_back(s[i]);\n            }\n            \n        }\n        return ans;\n    }\n};\n```
['String', 'Stack', 'C++']