16 lines concise and easy understand c++ solution


  • 0
    A
    class Solution {
    public:
        vector<vector<string>> groupStrings(vector<string>& strings) {
            unordered_map<string, vector<string>> mp;
            vector<vector<string>> res;
            for(auto str : strings) mp[getString(str)].push_back(str);
            for(auto str : mp){
                vector<string> strs = str.second;
                sort(strs.begin(), strs.end());
                res.push_back(strs);
            }
            return res;
        }
        string getString(string s){
            string res = "";
            int shift = s[0] - 'a';
            for(auto c : s){
                res = res + char(int(c - 'a' - shift + 26) % 26 + 'a');
            }
            return res;
        }
    };

Log in to reply
 

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