string removeDuplicates(string str, int k)
{
stack<pair<char, int>> s;
auto begin = str.begin(), end = str.end();
while (begin != end)
{
auto ch = *begin;
if (!s.empty())
{
auto& top = s.top();
if (top.first != ch)
s.push({ ch, 1 });
else
top.second++;
if (top.second == k)
s.pop();
}
else
s.push({ ch, 1 });
begin++;
}
str.clear();
while (!s.empty())
{
auto top = s.top();
for (int i = 0; i < top.second; i++)
str.push_back(top.first);
s.pop();
}
reverse(str.begin(), str.end());
return str;
}
'코딩테스트 > LeetCode' 카테고리의 다른 글
[LeetCode] 153. Find Minimum in Rotated Sorted Array (0) | 2024.03.05 |
---|---|
[LeetCode] Majority Element (0) | 2023.12.19 |
[LeetCode] Remove All Adjacent Duplicates In String (0) | 2023.12.12 |
[LeetCode] Valid Palindrome II (0) | 2023.12.11 |
[LeetCode] Excel Sheet Column Number (0) | 2023.12.11 |