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)


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