Super Reduced String

  • + 0 comments
    #include <bits/stdc++.h>
    #include <string>
    using namespace std;
    
    /*
     * Complete the 'superReducedString' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    string superReducedString(string s) {
        for(int i=1;i<s.length();i++){
            if(s[i]==s[i-1]){
                s.erase(s.begin()+i);
                s.erase((s.begin()+(i-1)));
            i=1;
                
            }
            if(s.length()==0){
                return "Empty String";
            }
            
        }
        if(s[0]==s[1]){
            return "Empty String";
        }
        return s;
    }
    
    int main()
    {
        ofstream fout(getenv("OUTPUT_PATH"));
    
        string s;
        getline(cin, s);
    
        string result = superReducedString(s);
    
        fout << result << "\n";
    
        fout.close();
    
        return 0;
    }