[Java/C++/Python] Two Pointers and Stack Solution
Solution 1: Two Pointers\ni refers to the index to set next character in the output string.\nj refers to the index of current iteration in the input string.\n\n
## Solution 1: Two Pointers\n`i` refers to the index to set next character in the output string.\n`j` refers to the index of current iteration in the input string.\n\nIterate characters of `S` one by one by increasing `j`.\n\nIf `S[j]` is same as the current last character `S[i - 1]`,\nwe remove duplicates by doing `i -= 2`.\n\nIf `S[j]` is different as the current last character `S[i - 1]`,\nwe set `S[i] = S[j]` and increment `i++`.\n<br>\n\n**Java**\n```java\n    public String removeDuplicates(String s) {\n        int i = 0, n = s.length();\n        char[] res = s.toCharArray();\n        for (int j = 0; j < n; ++j, ++i) {\n            res[i] = res[j];\n            if (i > 0 && res[i - 1] == res[i]) // count = 2\n                i -= 2;\n        }\n        return new String(res, 0, i);\n    }\n```\n\n**C++**\n```cpp\n    string removeDuplicates(string s) {\n        int i = 0, n = s.length();\n        for (int j = 0; j < n; ++j, ++i) {\n            s[i] = s[j];\n            if (i > 0 && s[i - 1] == s[i]) // count = 2\n                i -= 2;\n        }\n        return s.substr(0, i);\n    }\n```\n<br>\n\n## Why This solution\nYou can easily update this solution to remove more duplicates.\nNow it\'s a specil case where we only remove replicates `k = 2`.\n<br>\n\n## Solution 2: Stack\n\nKeep a `res` as a characters stack.\nIterate characters of `S` one by one.\n\nIf the next character is same as the last character in `res`,\npop the last character from `res`.\nIn this way, we remove a pair of adjacent duplicates characters.\n\nIf the next character is different,\nwe append it to the end of `res`.\n<br>\n\n**C++**\n```cpp\n    string removeDuplicates(string S) {\n        string res = "";\n        for (char& c : S)\n            if (res.size() && c == res.back())\n                res.pop_back();\n            else\n                res.push_back(c);\n        return res;\n    }\n```\n**Java**\n```java\n    public String removeDuplicates(String S) {\n        StringBuilder sb = new StringBuilder();\n        for (char c : S.toCharArray()) {\n            int size = sb.length();\n            if (size > 0 && sb.charAt(size - 1) == c) {\n                sb.deleteCharAt(size - 1);\n            } else {\n                sb.append(c);\n            }\n        }\n        return sb.toString();\n    }\n```\n**Python**\n```py\n    def removeDuplicates(self, S):\n        res = []\n        for c in S:\n            if res and res[-1] == c:\n                res.pop()\n            else:\n                res.append(c)\n        return "".join(res)\n```\n\n**Python 1-line**\n```py\n    def removeDuplicates(self, S):\n        return reduce(lambda s, c: s[:-1] if s[-1:] == c else s + c, S)\n```\n## **Complexity**\nThese two solution actually work in the similar way.\nTime `O(N)` for one pass\nSpace `O(N)` for output\n<br>
[]