LeetCode10.02(2021.7.18)

LC面试题 10.02. 变位词组

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        map<string, vector<string>> mp;
        for (string str: strs) {
            string key = str;
            sort(key.begin(), key.end());
            mp[key].push_back(str);
        }
        vector<vector<string>> ans;
        for (auto it = mp.begin(); it != mp.end(); ++it)
            ans.push_back(it->second);
        return ans;
    }
};

发表评论