0%

1047. Remove All Adjacent Duplicates In String

O(n) time O(n) space
这道题的意思是一定要『一对』才remove

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
string removeDuplicates(string S) {
string res;
for (char c : S) {
if (!res.empty() && res.back() == c) {
res.pop_back();
} else {
res += c;
}
}
return res;
}
};