Leetcode 49 Group Anagrams

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010821666/article/details/82749629

Leetcode 49 Group Anagrams

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
      unordered_map<string,multiset<string>> map;//should be multiset not set,for repetition
      
      vector<vector<string>> res;
        if (strs.empty()) {
            return res;
        }
      for(auto str : strs){
	string temp = str;
	sort(temp.begin(),temp.end());
	map[temp].insert(str);
      }
      
      for(auto strSet :map){
	vector<string> singleRes(strSet.second.begin(),strSet.second.end());
	res.push_back(singleRes);
      }
      
      return res;

    }
};

猜你喜欢

转载自blog.csdn.net/u010821666/article/details/82749629