unordered_map solution


  • 0
    T
    class Solution {
    public:
        vector<vector<string>> groupStrings(vector<string>& strings) {
            unordered_map<string, vector<string>> pattern;
            for (string &s:strings) {
                string p;
                for (int i = 1, l = s.size(); i < l; ++i) {
                    p += to_string((26+s[i]-s[i-1])%26);
                }
                pattern[p].push_back(s);
            }
            vector<vector<string>> rst;
            for (auto it = pattern.begin(); it != pattern.end(); it++) {
                rst.push_back(it->second);
            }
            return rst;
        }
    };
    

Log in to reply
 

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