Group Anagrams

Medium Subject: Arrays & Hashing
Time Complexity
O(N * K log K)
Space Complexity
O(N * K)

Problem Description

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

An anagram is a word formed by rearranging the letters of a different word, using all the original letters exactly once.

Optimal Solution

Python
from collections import defaultdict

def groupAnagrams(strs):
    groups = defaultdict(list)
    for s in strs:
        key = ''.join(sorted(s))
        groups[key].append(s)
    return list(groups.values())