✅Beats 100% || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥
\n# Intuition:\n\nThe intuition is to group words that are anagrams of each other together. Anagrams are words that have the same characters but in a different 
\n# Intuition:\n\nThe intuition is to group words that are anagrams of each other together. Anagrams are words that have the `same` characters but in a `different` order.\n\n# Explanation:\n\nLet\'s go through the code step by step using the example input `["eat","tea","tan","ate","nat","bat"]` to understand how it works.\n\n1. **Initializing Variables**\n    - We start by initializing an empty unordered map called `mp` (short for map), which will store the groups of anagrams.\n\n2. **Grouping Anagrams**\nWe iterate through each word in the input vector `strs`. Let\'s take the first word, "eat", as an example.\n\n    - **Sorting the Word**\nWe create a string variable called `word` and assign it the value of the current word ("eat" in this case). \n\n        Next, we sort the characters in `word` using the `sort()` function. After sorting, `word` becomes "aet". \n\n    - **Grouping the Anagram**\nWe insert `word` as the key into the `mp` unordered map using `mp[word]`, and we push the original word ("eat") into the vector associated with that key using `mp[word].push_back(x)`, where `x` is the current word.\n\n        Since "aet" is a unique sorted representation of all the anagrams, it serves as the key in the `mp` map, and the associated vector holds all the anagrams. \n\nFor the given example, the `mp` map would look like this after processing all the words:\n```\n{\n  "aet": ["eat", "tea", "ate"],\n  "ant": ["tan", "nat"],\n  "abt": ["bat"]\n}\n```\n\n3. **Creating the Result**\nWe initialize an empty vector called `ans` (short for answer) to store the final result.\n\n    - We iterate through each key-value pair in the `mp` map using a range-based for loop. For each pair, we push the vector of anagrams (`x.second`) into the `ans` vector.\n\nFor the given example, the `ans` vector would look like this:\n```\n[\n  ["eat", "tea", "ate"],\n  ["tan", "nat"],\n  ["bat"]\n]\n```\n\n4. **Returning the Result**\nWe return the `ans` vector, which contains the groups of anagrams.\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    vector<vector<string>> groupAnagrams(vector<string>& strs) {\n        unordered_map<string, vector<string>> mp;\n        \n        for(auto x: strs){\n            string word = x;\n            sort(word.begin(), word.end());\n            mp[word].push_back(x);\n        }\n        \n        vector<vector<string>> ans;\n        for(auto x: mp){\n            ans.push_back(x.second);\n        }\n        return ans;\n    }\n};\n```\n```Java []\nclass Solution {\n    public List<List<String>> groupAnagrams(String[] strs) {\n        Map<String, List<String>> map = new HashMap<>();\n        \n        for (String word : strs) {\n            char[] chars = word.toCharArray();\n            Arrays.sort(chars);\n            String sortedWord = new String(chars);\n            \n            if (!map.containsKey(sortedWord)) {\n                map.put(sortedWord, new ArrayList<>());\n            }\n            \n            map.get(sortedWord).add(word);\n        }\n        \n        return new ArrayList<>(map.values());\n    }\n}\n```\n```Python3 []\nclass Solution:\n    def groupAnagrams(self, strs):\n        anagram_map = defaultdict(list)\n        \n        for word in strs:\n            sorted_word = \'\'.join(sorted(word))\n            anagram_map[sorted_word].append(word)\n        \n        return list(anagram_map.values())\n```\n\n![CUTE_CAT.png](https://assets.leetcode.com/users/images/86270fe9-da59-416b-a22e-21b787b13712_1687773427.3564198.png)\n\n**If you are a beginner solve these problems which makes concepts clear for future coding:**\n1. [Two Sum](https://leetcode.com/problems/two-sum/solutions/3619262/3-method-s-c-java-python-beginner-friendly/)\n2. [Roman to Integer](https://leetcode.com/problems/roman-to-integer/solutions/3651672/best-method-c-java-python-beginner-friendly/)\n3. [Palindrome Number](https://leetcode.com/problems/palindrome-number/solutions/3651712/2-method-s-c-java-python-beginner-friendly/)\n4. [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/solutions/3666304/beats-100-c-java-python-beginner-friendly/)\n5. [Remove Element](https://leetcode.com/problems/remove-element/solutions/3670940/best-100-c-java-python-beginner-friendly/)\n6. [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/solutions/3672475/4-method-s-c-java-python-beginner-friendly/)\n7. [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/solutions/3675747/beats-100-c-java-python-beginner-friendly/)\n8. [Majority Element](https://leetcode.com/problems/majority-element/solutions/3676530/3-methods-beats-100-c-java-python-beginner-friendly/)\n9. [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/3676877/best-method-100-c-java-python-beginner-friendly/)\n10. [Valid Anagram](https://leetcode.com/problems/valid-anagram/solutions/3687854/3-methods-c-java-python-beginner-friendly/)\n11. [Group Anagrams](https://leetcode.com/problems/group-anagrams/solutions/3687735/beats-100-c-java-python-beginner-friendly/)\n12. **Practice them in a row for better understanding and please Upvote for more questions.**\n\n**If you found my solution helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions.**
['Hash Table', 'Sorting', 'C++', 'Java', 'Python3']