LeetCode_Anagrams

2625 ワード

Given an array of strings, return all groups of strings that are anagrams.



Note: All inputs will be in lower-case.


  
class Solution {

private:

    vector<string>  result;

    map<string, vector<string>> m;

public:

    vector<string> anagrams(vector<string> &strs) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        result.clear();

        m.clear();



        int n = strs.size();

        for(int i= 0; i< n;i++)

        { 

           string tmp = strs[i];

           sort(tmp.begin(), tmp.end()) ;

           m[tmp].push_back(strs[i]);

        }

        for(map<string, vector<string>>::iterator iter= m.begin(); iter != m.end(); iter++)

        {

            if((iter->second).size() >1)

            {

              for(int i= 0; i < (iter->second).size() ; i++    )          

                result.push_back((iter->second)[i]);

            }

        }

        return result;

        

    }

};