My Updated Version


Neetcode Version

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        dic = defaultdict(list)
 
        for s in strs:
            count = [0] * 26
 
            for c in s:
                count[ord(c) - ord('a')] += 1
 
            dic[tuple(count)].append(s)
 
        return dic.values()

Time Limit Exceeded Version

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        sDict = dict()
        tDict = dict()
 
        for c in s:
            if c in sDict.keys():
                sDict[c] += 1
            else:
                sDict[c] = 1
        
        for c in t:
            if c in tDict.keys():
                tDict[c] += 1
            else:
                tDict[c] = 1
        
        if len(sDict.keys()) != len(tDict.keys()):
            return False
 
        for c in sDict.keys():
            if (c not in tDict.keys()) or (tDict[c] != sDict[c]):
                return False
        
        return True
 
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        res = list()
        for s in strs:
            res.append([s])
 
        deletedIndices = set()
 
        for i in range(len(strs)):
            for j in range(len(strs)):
                if i != j and (j not in deletedIndices) and (i not in deletedIndices):
                    if self.isAnagram(strs[i], strs[j]):
                        res[i].append(strs[j])
                        deletedIndices.add(j)
 
        resCopy = []
        for r in res:
            resCopy.append(r)
 
        for index in deletedIndices:
            resCopy.remove(res[index])            
 
        return resCopy

References

https://leetcode.com/problems/group-anagrams/