Super Reduced String

  • + 0 comments

    Best and performant approach in C++. Behold my simple C++ solution -

    string superReducedString(string s) {
        int i=1;
        while (i<s.length()) {
            if (s[i] == s[i-1]) {
                s.erase(i-1,2);
                i=1;
            } else {
                i++;
            }
        }
        
        if (s.empty()) {
            return "Empty String";
        } else {
            return s;
        }
    }