#include using namespace std; // } Driver Code Ends class Solution { public: // Remove all the consecutive duplicate pairs and concatenate the // remaining string to replace the original string. This operation // can be applied any number of times string removePair(string s) { // code here string output; size_t i = 1; for (auto c: s) { if (output.size() && output.back() == c) output.pop_back(); else output.push_back(c); cout << s.substr(0, i++) << " output: " << output << endl; } return output.size() ? output : "-1"; } }; int main() { Solution obj; vector> v = { {"aaabbaaccd", "ad"}, // Remove (aa)abbaaccd => abbaaccd // Remove a(bb)aaccd => aaaccd // Remove (aa)accd => accd // Remove a(cc)d => ad {"xiiccccssxxxgggww", "g"} // Remove x(ii)ccccssxxxgggww => xccccssxxxgggww // Remove x(cc)ccssxxxgggww => xccssxxxgggww // Remove x(cc)ssxxxgggww => xssxxxgggww // Remove x(ss)xxxgggww => xxxxgggww // Remove (xx)xxgggww => xxgggww // Remove (xx)gggww => gggww // Remove (gg)gww => gww // Remove g(ww) => g }; for (auto i: v) { string res = obj.removePair(get<0>(i)); cout << res << endl; cout << get<1>(i) << endl; cout << "------------" << endl; } } // } Driver Code Ends