⭐ FASTEST WAY TO SOLVE THIS QUESTION || BEATS 97.36% ⭐
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires grouping anagrams together in a given array of strings. Two string
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires grouping anagrams together in a given array of strings. Two strings are considered anagrams if they have the same characters, but in a different order.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create an empty hash map, map, to store the grouped anagrams.\n\n2. Iterate through each string, str, in the input array strs:\n\n3. Convert str into a character array, chars.\n\n4. Sort chars in non-decreasing order.\n\n5. Convert chars back to a string, sortedStr.\n\n6. If sortedStr is not present in map, add it as a new key with an empty list as the value.\n\n7. Append str to the list associated with sortedStr in map.\n\n8. Return a new ArrayList containing all the values (lists of anagrams) from map.\n\n# Complexity\n- Time complexity:O(n * m * log m), where n is the number of strings in the input array and m is the maximum length of a string. Sorting each string takes O(m * log m) time, and we do this for each of the n strings.\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n * m), the space used by the hash map map and the returned ArrayList, where n is the number of strings and m is the maximum length of a string.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public List<List<String>> groupAnagrams(String[] strs) {\n        if (strs.length == 0) {\n            return new ArrayList<>();\n        }\n        \n        Map<String, List<String>> map = new HashMap<>();\n        \n        for (String i : strs) {\n            char[] chars = i.toCharArray();\n            Arrays.sort(chars);\n            String sortedStr = String.valueOf(chars);\n            \n            if (!map.containsKey(sortedStr)) {\n                map.put(sortedStr, new ArrayList<>());\n            }\n            \n            map.get(sortedStr).add(i);\n        }\n        \n        return new ArrayList<>(map.values());\n    }\n}\n\n```
['Java']