Super Reduced String

  • + 0 comments

    C++ 20:

    string onestepReduction (string s){
        int n = s.size();
        string reduced = "";
        int count;
        
        for (int i = 0; i < n-1; i++){
            if (s[i] != s[i+1]){
                reduced += s[i];
                count = i;
            }
            else{
                count = i+1;
                break;
            }
        }
        
        for (int j = count+1; j < n; j++) reduced += s[j];
        return reduced;
    }
    
    string superReducedString(string s) {
        string reduced = onestepReduction(s);
        
        while (reduced != s && reduced != ""){
            s = reduced;
            reduced = onestepReduction(s);
        }
        if (reduced == "") return "Empty String";
        else return reduced;
    }