C++_Accpeted_Map


  • 0

    Key point is that finding out the character of each string.

    class Solution {
    public:
    vector<vector<string>> groupStrings(vector<string>& strings) {
        unordered_map<string, vector<string>> mp;
        for(auto str: strings){
            string KEY = "";
            for(int i = 1; i < str.size(); ++i){
                KEY += to_string(str[i] - str[i-1] < 0 ? str[i] - str[i-1] + 26 : str[i] - str[i-1]) + ".";
            }
            mp[KEY].push_back(str);
        }
        
        vector<vector<string>> res;
        vector<string> KEYS;
        for(auto k : mp){
            KEYS = k.second;
            res.push_back(KEYS);
        }
        return res;
    }
    };

Log in to reply
 

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