LeetCode 049 Group Anagrams

LeetCode 049 Group Anagrams Problem

Download Code
class Solution(object):
    # def groupAnagrams(self, strs):
    #     """
    #     :type strs: List[str]
    #     :rtype: List[List[str]]
    #     """
    #     hash = {}
    #     for s in strs:
    #         key = self.hash_key(s)
    #         try:
    #             hash[key].append(s)
    #         except KeyError:
    #             hash[key] = [s]
    #     for k, v in hash.items():
    #         if len(v) > 1:
    #             # sort
    #             v.sort()
    #     return hash.values()
    #
    # def hash_key(self, s):
    #     # hash string with 26 length array
    #     table = [0] * 26
    #     for ch in s:
    #         index = ord(ch) - ord('a')
    #         table[index] += 1
    #     return str(table)

    def groupAnagrams(self, strs):
        strs.sort()
        hash = {}
        for s in strs:
            key = self.hash_key(s)
            try:
                hash[key].append(s)
            except KeyError:
                hash[key] = [s]
        return hash.values()

    def hash_key(self, s):
        # hash string with 26 length array
        table = [0] * 26
        for ch in s:
            index = ord(ch) - ord('a')
            table[index] += 1
        return str(table)
Download Group Anagrams.py

List of all Group Anagrams problems

Leetcode 049 Group Anagrams problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.