✅  Remove All Adjacent Duplicates In String | Simple Solutions w/ Explanation
Solution 1:(Recursion-TLE)\n\nBrute force way is to simply remove the characters from string whenever you find any adjacent duplicates,\nand then again restart 
***Solution 1:(Recursion-TLE)***\n```\nBrute force way is to simply remove the characters from string whenever you find any adjacent duplicates,\nand then again restart from start.\n\n```\n\n```\nclass Solution {\npublic:\n    string ans;\n    void go(string str,int idx=0)\n    {\n        if(idx>=str.length())\n        {\n            ans=str;\n            return;\n        }\n        if(str[idx]==str[idx+1])\n        {\n            if(idx==0)\n                go(str.substr(idx+2),0);\n            else\n                go(str.substr(0,idx)+str.substr(idx+2),0);\n        }\n        else\n        go(str,idx+1);\n    }\n    string removeDuplicates(string s) \n    {\n        ans="";\n        go(s);\n        return ans;\n    }\n};\n```\n\n***Solution 2:(Using Stack- Accepted)***\n```\n# Optimised Approach:\n\n1) We will be using stack for it.\n\n2) We will traverse the characters in the string one by one and compare with stack top,\nif at any time stack.top()==currentCharacter then we pop the element from stack.\n\n3) And at last we will form the answer string using the stack elements.\n\nLet us simulate the process with the help of an eg:\nstring : abbaca\nstack: empty\n\nfirst iteration: \'a\'\nstack is empty so we will push \'a\' to the stack,\nstack: |a|\n\nsecond iteration: \'b\'\nstack.top() is not equal to \'b\',\nso we will push \'b\' to the stack.\nstack: |b|a|\n\nthird iteration: \'b\'\nstack.top() is equal to \'b\',\nso we will pop the element from stack.\nstack: |a|\n\nfourth iteration: \'a\'\nstack.top() is equal to \'a\',\nso we will pop element from the stack.\nstack: empty\n\nFifth iteration: \'c\'\nstack is empty\nso we will push the element into the stack.\nstack: |c|\n\nSixth iteration: \'a\'\nstack.top() is not equal to \'a\',\nso we will push \'a\' to the stack.\nstack: |a|c|\n\nNow, we will pop all the elements from the stack and store them into the answer string,\nans = "ac"\nas the elements are in reverse order and hence we need to reverse the ans,\nreverse(ans)\nans="ca" (FINAL RESULT)\n```\n\n**Time Complexity: O(N)**\n**Space Complexity: O(N)**\n**N: length of string**\n\n```\nclass Solution {\npublic:\n    \n    string removeDuplicates(string str) \n    {\n        stack<char> s;\n        for(auto i:str)\n        {\n            if(s.empty())\n                s.push(i);\n            else\n            {\n                if(s.top()==i)\n                {\n                    s.pop();\n                }\n                else\n                    s.push(i);\n            }\n        }\n        string ans;\n        while(!s.empty())\n            ans+=s.top(),s.pop();\n        reverse(ans.begin(),ans.end());\n        return ans;\n    }\n};\n```\n\n***Solution 3:(Using String as Stack - Accepted)***\n```\nIt is just the simple modification of above code,\ninstead of using stack we can directly use the resultant string for performing pop and push operations.\n```\n```\nclass Solution {\npublic:\n    \n    string removeDuplicates(string str) \n    {\n        string ans;\n        for(auto i:str)\n        {\n            if(!ans.empty() and ans.back()==i)\n                ans.pop_back();\n            else\n                ans.push_back(i);\n        }\n        return ans;\n    }\n};\n```\n***If you found the post useful, then please do consider it for Upvotes\uD83D\uDE00\uD83D\uDE00***\n
['C']