Leetcode: Group Anagrams

Given an array of strings, group anagrams together.



Similar Problems:


Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], 
Return:

[
  ["ate", "eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Note: All inputs will be in lower-case.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


## Blog link: https://code.dennyzhang.com/group-anagrams
## Basic Ideas:  map each item to a string, group them by a map, dump the map
## Complexity: Time O(n*k*log(k)), Space O(k*n). k is the length of longest item
class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        m = {}
        for item in strs:
            sorted_item = ''.join(sorted(item))
            if m.has_key(sorted_item):
                m[sorted_item].append(item)
            else:
                m[sorted_item] = [item]

        res = []
        for key in m:
            res.append(m[key])
        return res
linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.