My 20 lines’ solution which uses c++ map.


  • 0
    class Solution {
    public:
    	string stringToOrderStr(string str)
    	{
    		string res = "";
    		sort(str.begin(),str.end());
    		return str;
    	}
    	vector<vector<string>> groupAnagrams(vector<string>& strs) {
    		map<string,vector<string>>classfication;
    		for (int i = 0; i < strs.size(); i++){
    			string str = stringToOrderStr(strs[i]);
    			classfication[str].push_back(strs[i]);
    		}
    		vector<vector<string>>ans;
    		for(auto &p : classfication){
    			sort(classfication[p.first].begin(), classfication[p.first].end());
    			ans.push_back(classfication[p.first]);
    		}
    		return ans;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.