leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1209.cpp (521B)


      1 class Solution {
      2   public:
      3     string removeDuplicates(string s, int k) {
      4         stack<pair<char, int>> st;
      5         for (char c : s)
      6             if (!st.empty() && st.top().first == c) {
      7                 if (++st.top().second == k) st.pop();
      8             } else
      9                 st.push(make_pair(c, 1));
     10 
     11         string res = "";
     12         while (!st.empty()) {
     13             res += string(st.top().second, st.top().first);
     14             st.pop();
     15         }
     16         reverse(res.begin(), res.end());
     17         return res;
     18     }
     19 };