simple solution with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nWe have to return the group of anagrams from the given list.\nAnagrams are words or phr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe have to return the group of anagrams from the given list.\nAnagrams are words or phrases that are formed by rearranging the letters of another word or phrase, using all the original letters exactly once. In other words, two words are anagrams if they contain the same characters in the same frequency but in a different order.\nexample:- "listen" and "silent": Both words use the letters L, I, S, T, E, N exactly once.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMy approach for this problem is very simple.\nFirst of all i will convert the char to array.\nAnd then we will sort the array.\nThis will result us to get all the elements to the lexicographically.\n```\nchar[] chars = s.toCharArray();\n            Arrays.sort(chars);\n```\nSo the use of above lines are that for example if we have the two words called, bat and tab.\nWhen we order them in lexicographically manner then we will get both words as abt and abt.\nFrom this we will get to know that both are anagrams.\nNow coming to the main logic of the problem:-\n```\nif (!ans.containsKey(key)) {\n                ans.put(key, new ArrayList<>());\n            }\n            ans.get(key).add(s);\n        }\n\n```\nCheck if the HashMap already has an entry for the key. If not, add a new entry with the key and an empty list as the value.\nAdd the original string s to the list associated with the key.\n\n```\nreturn new ArrayList<>(ans.values());\n```\nans.values() returns a collection of all the values in the HashMap (i.e., all the lists of anagrams).\nnew ArrayList<>(...) converts this collection into an ArrayList of lists, which is the expected return type of the method.\n\nFor an input of ["eat", "tea", "tan", "ate", "nat", "bat"], the function will:\n\nSort "eat", "tea", and "ate" to the key "aet" and group them together.\nSort "tan" and "nat" to the key "ant" and group them together.\nSort "bat" to the key "abt" and create its own group.\nThe final output will be: [[eat, tea, ate], [tan, nat], [bat]].\n\n\nThis code effectively groups anagrams by sorting each string to create a common key for all anagrams, which are then stored in a HashMap. The result is a list of lists, where each inner list contains strings that are anagrams of each other.\n# Complexity\n- Time complexity: O(N\u22C5KlogK)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N\u22C5K)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\nThis analysis assumes that N is the number of strings and K is the maximum length of a string in the input array.\n\n# Code\n```java []\nclass Solution {\n    public List<List<String>> groupAnagrams(String[] strs) {\n        Map<String, List<String>> ans = new HashMap<>();\n\n        for (String s : strs) {\n            char[] chars = s.toCharArray();\n            Arrays.sort(chars);\n            String key = new String(chars);\n            if (!ans.containsKey(key)) {\n                ans.put(key, new ArrayList<>());\n            }\n            ans.get(key).add(s);\n        }\n\n        return new ArrayList<>(ans.values());        \n    }\n}\n```\n```cpp []\nclass Solution {\npublic:\n    vector<vector<string>> groupAnagrams(vector<string>& strs) {\n        unordered_map<string, vector<string>> ans;\n\n        for (string& s : strs) {\n            string key = s;\n            sort(key.begin(), key.end());\n            ans[key].push_back(s);\n        }\n\n        vector<vector<string>> result;\n        for (auto& entry : ans) {\n            result.push_back(entry.second);\n        }\n\n        return result;        \n    }\n};\n```\n```python3 []\nclass Solution:\n    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:    \n        ans = defaultdict(list)\n\n        for s in strs:\n            key = "".join(sorted(s))\n            ans[key].append(s)\n        \n        return ans.values()\n```\n![d971a77c-9c71-4405-86b6-2727fd435122_1720044713.1503446.jpeg](https://assets.leetcode.com/users/images/2560104b-cb36-4c1b-a040-cfb9a61aa3bb_1728793048.7958784.jpeg)\n\n
['Array', 'C++', 'Java', 'Python3']