✅C++ solution using soring and without sorting by HashMap with time complexity analysis!
If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u27
> **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u2764\uFE0F**\n> \n\n\n---\n\n## Explanation:\n\n### Solution 01\n\n- Solution using ***HashMap with sorting.***\n- Here we store all same strings in a vector inside Hashmap.\n- We\u2019ll take every string from strs in temp then sort that by character. Push the main string to the map.\n- Like these elements with the same character will push to the same vector of hashmap.\n- Then push all the vectors from map to ans.\n- **Time complexity:** O(n.klogk) /***k**\xA0is the length of string and\xA0**n**\xA0is the total no. of strings*\n\n### Solution 02\n\n- Solution using ***Map without sorting.***\n- Here we\u2019ll do the same but this time we\u2019ll take a map of map to skip the sorting part.\n- If we could use an unordered map then time complexity could reduce to O(nk), but as it takes O(logn) time for any operations, so time takes same as solution 1.\n- Here we count the characters in map and push it to main map.\n- **Time complexity:** O(n.klogk) /*insertion into a map takes **logK** time*\n\n---\n\n## Code:\n\n```cpp\n//Solution 01:\nclass Solution {\npublic:\n    vector<vector<string>> groupAnagrams(vector<string>& strs) {\n        int n = strs.size();\n        unordered_map<string, vector<string>> mp;\n        vector<vector<string>>ans;\n        \n        int i=0;\n        while(i<n){\n            string a = strs[i];\n            sort(a.begin(), a.end());\n            mp[a].push_back(strs[i]);\n            i++;\n        }\n        for(auto i=mp.begin(); i!= mp.end(); i++){\n            ans.push_back(i->second);\n        }\n        return ans;\n        \n    }\n};\n\n//Solution 02:\nclass Solution {\npublic:\n    vector<vector<string>> groupAnagrams(vector<string>& strs) {\n        int n = strs.size();\n        map<map<char, int>, vector<string>> mp;\n        vector<vector<string>>ans;\n        \n        int i=0;\n        while(i<n){\n            map<char, int> a;\n            for(int j=0; j<strs[i].size(); j++)\n                a[strs[i][j]]++;\n            mp[a].push_back(strs[i]);\n            i++;\n        }\n        for(auto i=mp.begin(); i!= mp.end(); i++){\n            ans.push_back(i->second);\n        }\n        return ans;\n        \n    }\n};\n```\n\n---\n\n> **Please upvote this solution**\n>
['C', 'C++']