[Python] 2 solutions, O(nk*log k) and O(nk + 26n), explained
Solution 1\nFirst idea is to notice that if we have two anagrams, than when we sort symbols in each of them, then we will have exactly the same string. So we ne
#### Solution 1\nFirst idea is to notice that if we have two anagrams, than when we sort symbols in each of them, then we will have exactly the same string. So we need for each string to sort it and then use defaultdict. \n\n#### Complexity\nTime complexity will be `O(nk * log k)`, space complexity is `O(nk)`.\n\n#### Code\n```python\nclass Solution:\n    def groupAnagrams(self, strs):\n        t = defaultdict(list)\n        for s in strs:\n            t["".join(sorted(s))].append(s)\n        return t.values()\n```\n\n#### Solution 2\nTwo strings are anagrams if and only if their character counts, that is frequencies of each letter `a, b, ..., z` are the same. So it can be done with `defauldict(list)`, where key is 26-element list and values are strings, corresponding to this key.\n\n#### Complexity\nTime complexity is `O(nk + 26n)`, where `n` is number of strings and `k` is the length of the biggest string. Space complexity is `O(26n)`.\n\n#### Code\n```python\nclass Solution:\n    def groupAnagrams(self, strs):\n        ans = defaultdict(list)\n        for s in strs:\n            count = [0] * 26\n            for c in s:\n                count[ord(c) - ord(\'a\')] += 1\n            ans[tuple(count)].append(s)\n        return ans.values()\n```\n\nIf you have any questions, feel free to ask. If you like solution and explanations, please **Upvote!**
['Hash Table', 'Sorting']