Group Anagrams

 

49. Group Anagrams

Solution 1

  1. Get sorted chars array in each str.

  2. Store the strs with the same chars array into the same dictionary item.

def groupAnagrams(self, strs):
    """
    :type strs: List[str]
    :rtype: List[List[str]]
    """
    dic = {}
    for s in strs:
        chars = ''.join(sorted(s))
        if chars in dic:
            dic[chars].append(s)
        else:
            dic[chars] = [s]
    
    res = []
    for key in dic:
        res.append(dic[key])
    return res

Solution 2

  1. Get the number of appearance of each characters in the array.

  2. Use the count as key.

    def groupAnagrams(self, strs):
     ans = collections.defaultdict(list)
     for s in strs:
         ans[tuple(sorted(s))].append(s)
     return ans.values()