Two Pointer Solution - Clean Code
We can use two-pointer method to solve this problem. We can set a pointer p to point the next index of current substring:\n1. If there are no 2 same adjacent ch
We can use two-pointer method to solve this problem. We can set a pointer p to point the next index of current substring:\n**1. If there are no 2 same adjacent characters, then we move p forward along with i;\n2. else, we move back for 1 step since we need to remove those 2 same adjacent characters!**\n\n```\n    public String removeDuplicates(String S) {\n        char[] ch = S.toCharArray();\n        int len = S.length(), p = 0; // p as a pointer, to point the next index of current substring.\n        for (int i=0; i<len; i++) {\n            if (p == 0 || ch[i] != ch[p-1]) {\n                ch[p++] = ch[i];\n            } else {\n                p--;\n            }\n        }\n        return String.valueOf(ch).substring(0, p);\n    }\n```
[]