每个string sort。。。然后这个作为group的key

 

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        unordered_map<string , vector<string> >group;
        for(int i = 0 ; i < strs.size() ; i++) {
            string k = strs[i];
            sort(k.begin() , k.end());
            group[k].push_back(strs[i]);
        }
        
        vector<string> ans;
        for(auto it = group.begin() ; it != group.end() ; it++) {
            if(it -> second.size() > 1)
              ans.insert(ans.end() , it->second.begin() , it -> second.end());
        }
        return ans;
    }
};

 

相关文章:

  • 2022-12-23
  • 2021-12-20
  • 2021-09-22
  • 2021-05-18
  • 2021-07-01
  • 2021-09-30
猜你喜欢
  • 2021-08-10
  • 2022-02-15
  • 2022-01-18
  • 2022-01-31
  • 2021-08-12
  • 2021-09-16
  • 2021-11-24
  • 2021-09-17
相关资源
相似解决方案