diff options
author | terminaldweller <thabogre@gmail.com> | 2022-05-06 01:51:07 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-05-06 01:51:07 +0000 |
commit | c3539e88c994d246871709028ee4318a1a279f98 (patch) | |
tree | d48955e17f510ece7c7213af69b35d42777d72ed /1209/main.cpp | |
parent | update (diff) | |
download | leetcode-c3539e88c994d246871709028ee4318a1a279f98.tar.gz leetcode-c3539e88c994d246871709028ee4318a1a279f98.zip |
update
Diffstat (limited to '1209/main.cpp')
-rw-r--r-- | 1209/main.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/1209/main.cpp b/1209/main.cpp new file mode 100644 index 0000000..4b3017b --- /dev/null +++ b/1209/main.cpp @@ -0,0 +1,45 @@ + +#include "header.hpp" +#include <algorithm> +#include <stack> + +class Solution { +public: + static std::string removeDuplicates(std::string s, int k) { + std::stack<std::pair<unsigned char, int>> dict; + std::string result = ""; + + for (auto &iter : s) { + if (dict.empty()) { + dict.push(std::make_pair(iter, 1)); + continue; + } + if (dict.top().first == iter) { + dict.top().second++; + } else { + dict.push(std::make_pair(iter, 1)); + } + if (dict.top().second == k) { + dict.pop(); + } + } + + while (dict.size() > 0) { + result += dict.top().first; + if (dict.top().second == 1) + dict.pop(); + else + dict.top().second--; + } + std::reverse(result.begin(), result.end()); + return result; + } +}; + +int main(int argc, char **argv) { + std::cout << Solution::removeDuplicates("abcd", 2) << std::endl; + std::cout << Solution::removeDuplicates("deeedbbcccbdaa", 3) << std::endl; + std::cout << Solution::removeDuplicates("pbbcggttciiippooaais", 2) + << std::endl; + return 0; +} |