[C++] Regex Vs, Stack Solution, ~100% Time, ~100% Space
I know it sucks, just wanted to play with regexes in C++ for the sake of getting familiar with them in this language as well :)\n\ncpp\nclass Solution {\npublic
I know it sucks, just wanted to play with regexes in C++ for the sake of getting familiar with them in this language as well :)\n\n```cpp\nclass Solution {\npublic:\n    regex r = regex("(.)\\\\1");\n    \n    string removeDuplicates(string s) {\n        string oldS, newS = s;\n        while (oldS != newS) {\n            oldS = newS;\n            newS = regex_replace(oldS, r, "");\n        }\n        return newS;\n    }\n};\n```\n\n1.5 years later, a nicer one using the result string directly as a stack:\n\n```cpp\nclass Solution {\npublic:\n    string removeDuplicates(string s) {\n        // support variables\n        string res = "";\n        // parsing s \n        for (char c: s) {\n            // case 1: c is not a duplicate\n            if (!res.size() || res.back() != c) res.push_back(c);\n            // case 2: c is a duplicate\n            else res.pop_back();\n        }\n        return res;\n    }\n};\n```
['C', 'C++']