We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Super Reduced String
  5. Discussions

Super Reduced String

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • urossevkusic
    2 months ago+ 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());
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy