A Python solution without sort


  • 0
    C

    I think sort is slow. So I use a tuple as a key. But it's still not fast, which I think tuple() is slow too.

        def groupAnagrams(self, strs):
            """
            :type strs: List[str]
            :rtype: List[List[str]]
            """
            retdict = collections.defaultdict(list)
            for i in strs:
                nums =[0]*26
                for j in i:
                    nums[ord(j)-97] += 1
                retdict[tuple(nums)].append(i)
            return list(retdict.values())
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.