Anagrams

问题描述:
Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.
例如:输入为:{abc,bca,123,321,567}
输出为:{abc,bca,123,321}
解决方案:

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) {
        int len = strs.size();
        map<string,int> map1;
        vector<string> result;
        for(int i = 0; i< len;i++)
        {
            string temp = strs[i];
            sort(temp.begin(),temp.end());
            if(map1.count(temp)){
                if(map1[temp]>=0)
                {
                    result.push_back(strs[map1[temp]]);
                    map1[temp] = -1;
                    result.push_back(strs[i]);
                }else
                {
                    result.push_back(strs[i]);
                }
            }else{
                map1[temp] = i;
            }

        }
        return result;
    }
};

你可能感兴趣的:(C++,算法)