Review: Hashmap Problems

Review Hashmap code problems



  • Use a tuple as key

Group Shifted Strings

## Blog link: https://code.dennyzhang.com/group-shifted-strings
class Solution:
    def groupStrings(self, strings):
        """
        :type strings: List[str]
        :rtype: List[List[str]]
        """
        import collections
        m = collections.defaultdict(list)
        for s in strings:
            # ba -> (0, 25)
            # az -> (0, 25)
            tup = tuple([(ord(ch)-ord(s[0]))%26 for ch in s])
            m[tup].append(s)
        return [m[key] for key in m]

See all hashmap problems: #hashmap

See more blog_posts.

linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.