Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] Note:

All inputs will be in lowercase. The order of your output does not matter

My code:

class Solution:
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        result=collections.defaultdict(list)
        for item in strs:
            tmp_list=[]
            for i in range(len(item)):
                tmp_list.append(item[i])
            tmp_list.sort()
            reform_str=str(tmp_list)
            
            result[reform_str].append(item)
        record=[]
        for item in result.keys():
            record.append(result[item])
        return record

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