Anagrams

来源:互联网 发布:飞车a车性能数据 编辑:程序博客网 时间:2024/05/17 04:54
class Solution {public:    vector<string> anagrams(vector<string> &strs) {        unordered_map<string, vector<string> > group;                for(const auto &str: strs)        {            string key = str;            std::sort(key.begin(), key.end());            group[key].push_back(str);        }                vector<string> result;        for(auto it = group.cbegin(); it != group.cend(); it++)        {            if(it->second.size() > 1)                result.insert(result.end(), it->second.begin(), it->second.end());        }        return result;    }};


使用map对排序后的string进行分类处理。

0 0