-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1047.cpp
More file actions
35 lines (35 loc) · 777 Bytes
/
Copy path1047.cpp
File metadata and controls
35 lines (35 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution { // 344ms recursion
public:
string removeDuplicates(string S) {
int i = -1, j = 1;
for (; j < S.size(); ++j) {
if (S[j - 1] == S[j]) {
i = j - 1;
++j;
break;
}
}
return i == -1 ? S : removeDuplicates(S.substr(0, i) + S.substr(j));
}
};
class Solution2 { // 24ms iteration
public:
string removeDuplicates(string S) {
stack<int> st;
vector<bool> deleted(S.size());
for (int i = 0; i < S.size(); ++i) {
if (!st.empty() && S[st.top()] == S[i]) {
deleted[st.top()] = deleted[i] = true;
st.pop();
} else {
st.push(i);
}
}
string ret;
for (int i = 0; i < S.size(); ++i) {
if (!deleted[i])
ret += S[i];
}
return ret;
}
};