[leetcode]Anagrams

每个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;
    }
};

 

你可能感兴趣的:(LeetCode)