clueless coding // TODO: be smarter

LeetCode Group Shifted Strings: CPP Solution



TL;DR Code Solution


vector<vector<string>> groupStrings(vector<string>& strings) {

    unordered_map<string, vector<string>> map;
    for (int i = 0; i < strings.size(); i++) {
        string res = "";
        for (int j = 1; j < strings[i].length(); j++) {
            int key = (strings[i][j] - 'a') - (strings[i][j - 1] - 'a');
            if (key < 0) key += 26;
            res += key + 'a';
        }
        map[res].push_back(strings[i]);
    }

    vector<vector<string>> result;
    for (auto it = map.begin(); it != map.end(); it++) {
        result.push_back(it->second);
    }

    return result;

}