✅ Group Anagrams || Explanation W/ Images || Using Map || C++ | Python | Java
Pre-requisite:\nWhat is an Anagram?\nTwo strings are said to be anagrams of each other if they are made up of the same characters with same frequency. For examp
**Pre-requisite:**\n*What is an Anagram?*\nTwo strings are said to be anagrams of each other if they are made up of the same characters with same frequency. For example: the word `LISTEN` and `SILENT` are anagrams. They are composed of the letters `E, I, L, N, S, T`.\n`Insight-` The input is only `lowercase aplhabets`\n\n**Algorithm:**\n`Technique` - Group By Sorting\n*SAMPLE CASE-*\n![image](https://assets.leetcode.com/users/images/9e9dee48-ad03-4047-8a66-b11e8dbcac7f_1628753485.5853918.png)\n*SORTED SAMPLE CASE-*\n![image](https://assets.leetcode.com/users/images/12e377a3-1779-4e7c-ba97-f19fb5d390db_1628753533.013706.png)\n* Once we have made this observation, we create a map for the same where the `key` will be the sorted string, and the `value` would be the list of all the strings that are anagrams.\n* After iteration over all the strings, we get the desired group of anagrams\n* The time complexity of this approach depends on the sorting technique you use to sort the strings.\n\n**Solution:**\n`In C++`\n```\nclass Solution {\npublic:\n    vector<vector<string>> ans ;\n    vector<vector<string>> groupAnagrams(vector<string>& strs) {\n        vector<string> temp = strs ;\n        map<string, vector<string>> mp;\n        for(int i=0; i < temp.size() ; i++)\n\t\t{\n            string t = temp[i];\n            sort(temp[i].begin(), temp[i].end());\n            mp[temp[i]].push_back(t);\n        }\n        for(auto i:mp) ans.push_back(i.second);\n        return ans;\n    }\n};\n```\n`In Python3`\n```\nclass Solution:\n    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n        map_dict = {}\n        for i,string in enumerate(strs):\n            tmp = ("").join(sorted(string))\n            try:\n                map_dict[tmp].append(strs[i])\n            except:\n                map_dict[tmp] = [strs[i]]\n\n        return list(map_dict.values())\n```\n`In JAVA`\n```\nclass Solution {\n    public List<List<String>> groupAnagrams(String[] strs) {\n        Map<String ,List<String>> map = new HashMap<>();\n        for(int i=0;i<strs.length;i++)\n        {\n            char[] c = strs[i].toCharArray();\n            Arrays.sort(c) ;\n            String temp = String.valueOf(c);\n            if(map.containsKey(temp))\n            {\n                List<String> list = map.get(temp);\n                list.add(strs[i]);\n                map.put(temp,list);\n            }\n            else\n            { \n                List<String> list = new ArrayList<>();\n                list.add(strs[i]);\n                map.put(temp, list);\n            }            \n        }\n        List<List<String>> ans = new ArrayList<>();\n        ans.addAll(map.values());\n        return ans;\n    }\n}\n```\n**TIME COMPLEXITY - O(N*KlogK)**, where k is the length of the largest string. \n* This is because we are sorting string in every iteration.\n* It would be **different for different sorting techniques** for e.g. `O(N*K) for Counting Sort`\n\n**SPACE COMPLEXITY - O(N*K)**\n\n\n**SCRAP APPROACH:**\n`Brute Force-` Will give TLE on Large Testcases\n* Iterate over each string in the input array.\n* For each string, check with each element of the array if they are anagrams.\n* If it is an anagram, add it to a group.\n* Else, move the string to a different group\n\n**Do Consider the post for upvotes if you liked it. If you find any mistakes do let me know.\uD83D\uDE0A\nIf you are blindly downvoting I hope you get better and stop being jealous :)**
[]