C++ Group shift


  • 1
    M
    vector<vector<string>> groupStrings(vector<string>& strings) {
        vector<vector<string>> result;
        unordered_map<string, vector<string>> mp;
        for (auto str : strings) {
            mp[shift(str)].emplace_back(str);
        }
        
        vector<vector<string>> res;
        for (auto elem : mp) {
            if (elem.second.size() > 0) {
                sort(elem.second.begin(), elem.second.end());
                res.emplace_back(elem.second);
            }
        }
        return res;
    }
    
    string shift(const string &str) {
        string res = "";
        for (int i = 1; i < str.size(); ++i) {
            int diff = str[i] - str[i - 1];
            if (diff < 0) diff += 26;
            res += diff + 'a' + ',';
        }
        return res;
    }

Log in to reply
 

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