Skip to content

Commit ccf3354

Browse files
authored
Create remove-all-adjacent-duplicates-in-string-ii.cpp
1 parent 4676dbd commit ccf3354

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
string removeDuplicates(string s, int k) {
7+
vector<pair<char, int>> stk = {{'^', 0}};
8+
for (const auto& c : s) {
9+
if (stk.back().first == c) {
10+
if (++stk.back().second == k) {
11+
stk.pop_back();
12+
}
13+
} else {
14+
stk.emplace_back(c, 1);
15+
}
16+
}
17+
string result;
18+
for (const auto& [c, v] : stk) {
19+
result += string(v, c);
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)