C++ · problem solving

HackerRank “Super Reduce String”

Shil has a string, , consisting of lowercase English letters. In one operation, he can delete any pair of adjacent letters with same value. For example, string “” would become either “” or “” after operation.

Shil wants to reduce as much as possible. To do this, he will repeat the above operation as many times as it can be performed. Help Shil out by finding and printing ‘s non-reducible form!

Note: If the final string is empty, print .

Input Format

A single string, .


Output Format

If the final string is empty, print ; otherwise, print the final non-reducible string.

Sample Input 0


Sample Output 0


Sample Input 1


Sample Output 1

Empty String

Sample Input 2


Sample Output 2

Empty String


#include <iostream>
#include <string>
using namespace std;

int main()
    string s;
    getline(cin, s);
    int j;
    for (int i = 0; i < s.length()-1; i++)
       j = i + 1;
       if (s[i] == s[j])
         if (s.empty())
          cout << "Empty String" << endl;
          i = -1; 
 cout << s << endl;
 return 0;



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s