typedef std::list<std::string> List; typedef std::map<std::string, List> Map; Map getAnagrams(List& input) { Map result; for (const auto& s : input){ auto key = s; std::sort(key.begin(), key.end()); auto loc = result.find (key); if (loc != result.end ()){ loc->second.push_back (s); }else{ result.insert ({key, List{s}}); } } return result; }
时间: 2024-12-15 01:48:23