Java | TC: O(N) | SC: O(N) | Clean & Concise solution using StringBuilder as Stack
\n/**\n * Using StringBuilder as Stack.\n *\n * Time Complexity: O(N + N) = O(N) -> N for Iterating over the string. N for\n * sb.toString()\n *\n * Space Compl
```\n/**\n * Using StringBuilder as Stack.\n *\n * Time Complexity: O(N + N) = O(N) -> N for Iterating over the string. N for\n * sb.toString()\n *\n * Space Complexity: O(N) -> Space used by the StringBuilder\n *\n * N = Length of input string.\n */\nclass Solution {\n    public String removeDuplicates(String s) {\n        if (s == null) {\n            // Ask Interviewer about this base case. You can also return null here.\n            throw new IllegalArgumentException("Input string is null");\n        }\n\n        int len = s.length();\n        if (len <= 1) {\n            return s;\n        }\n\n        StringBuilder sb = new StringBuilder();\n\n        for (int i = 0; i < len; i++) {\n            char curChar = s.charAt(i);\n            int sbLen = sb.length();\n\n            if (sbLen > 0 && sb.charAt(sbLen - 1) == curChar) {\n                // If last character in StringBuilder is same as the current character, then\n                // remove the last character from StringBuilder\n                sb.setLength(sbLen - 1);\n            } else {\n                sb.append(curChar);\n            }\n        }\n\n        return sb.toString();\n    }\n}\n```\n\nSolution to Part II: [1209. Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/1490956/Java-or-TC:-O(N)-or-SC:-O(N)-or-Clean-and-Concise-solution-using-StringBuilder-and-Stack)
['String', 'Stack', 'Java']