easy python solution


  • 0
    H
    class Solution(object):
        def groupAnagrams(self, strs):
            """
            :type strs: List[str]
            :rtype: List[List[str]]
            """
            result = {}
            for str in strs:
                # list can not be key in a dict so choose tuple
                item = tuple(sorted(list(str)))
                if item not in result:
                    result[item] = []
                    result[item].append(str)
                else:
                    result[item].append(str)
            return list(result.values())

Log in to reply
 

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