C++ concise solution using unordered_map.


  • 1
    C
    vector<vector<string>> groupStrings(vector<string>& strings) {
        unordered_map<string, vector<string>> myMap;
        for (auto& s: strings) {
            string key = "";
            for (auto& c: s)
                key += to_string((c+26-s[0])%26)+',';
            myMap[key].push_back(s);
        }
        vector<vector<string>> res;
        for (auto it = myMap.begin(); it != myMap.end(); it++) {
            sort(it->second.begin(), (*it).second.end());
            res.push_back(it->second);
        }
        return res;
    }

Log in to reply
 

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