Super Reduced String

  • + 0 comments

    C++ solution

    std::list<char> super_reduced_list(std::list<char> &l) {
        int k = l.size();
        
        for(auto it1 = l.begin(), it2 = ++l.begin();
            (it1 != l.end()) && (it2 != l.end());
            ) {
            if(*it1 == *it2) {
                it2 = l.erase(it1);
                it1 = l.erase(it2);
                it2 = it1;
                it2++;
            } else {
    				it1++;
    				it2++;
    				}
        }
        
        for(char c : l) {
            std::cout << c;
        }
        
        std::cout << std::endl;
        
        if(k == l.size()) {
            return l;
        } else {
            return super_reduced_list(l);
        }
    }
    
    string superReducedString(string s) {
        std::list<char> l(s.begin(), s.end());
        super_reduced_list(l);
        
        return (l.empty())? "Empty String" : string(l.begin(), l.end());
    }