Remove All Adjacent Duplicates In String
[LeetCode] Remove All Adjacent Duplicates In String
string removeDuplicates(string str) { stack s; auto begin = str.begin(), end = str.end(); while (begin != end) { auto ch = *begin; if (!s.empty() && s.top() == ch) s.pop(); else s.push(ch); begin++; } str.clear(); while (!s.empty()) { str.push_back(s.top()); s.pop(); } reverse(str.begin(), str.end()); return str; }