Leetcode 249 Solution

This article provides solution to leetcode question 249 (group-shifted-strings)

https://leetcode.com/problems/group-shifted-strings

Solution

class Solution {
public:
    vector<vector<string>> groupStrings(vector<string>& strings) {
        map<vector<int>, vector<string>> m;

        for (auto str : strings)
        {
            vector<int> key;
            for (int i = 1; i < str.size(); i++)
                key.push_back((str[i] - str[i - 1] + 26) % 26);
            m[key].push_back(str);
        }

        vector<vector<string>> res;
        for (auto pair : m)
            res.push_back(pair.second);
        return res;
    }
};