leetcode-study

49. Group Anagrams

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        """
        Groups an array of strings into lists of anagrams.
        
        Parameters:
            strs (List[str]): List of strings to be grouped.
        
        Returns:
            List[List[str]]: A list where each sublist contains anagrams grouped together.
        """
        anagrams = {}  # Dictionary to hold groups of anagrams, key is sorted tuple of the string

        # Iterate over each string in the input list
        for s in strs:
            # Sort the string and convert to tuple to use as a hashable key
            key = tuple(sorted(s))
            # If the key exists in the dictionary, append the current string to the group.
            if key in anagrams:
                anagrams[key].append(s)
            else:
                # Create a new group for this key with the current string.
                anagrams[key] = [s]
                
        # Return all the grouped anagrams as a list of lists.
        return list(anagrams.values())

Summary of Techniques and Approaches: