Using STL multimaps


  • 0
    B

    This is the first time i have used a regular map in addition to an unordered_map. Now I know there some use of the regular map as well even with the advent of the hash map in STL !

        string frequencySort(string s) {
            unordered_map<char, int> mymap;
            for (char c: s)
              ++mymap[c];
              
            multimap<int, char> mynewmap;  //reverses the mymap
            for (auto x: mymap)
              mynewmap.insert(std::pair<int, char>(x.second,x.first));
            
            string res;
            for (auto x: mynewmap) //time to pre-pend strings
              res = std::string(x.first, x.second) + res;
            
            return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.