Python easy to understand solution with comments.

  • 4
    def groupStrings(self, strings):
        dic = {}
        for s in strings:
            # "abc"->(0,1,2), "az"->(0,25), etc 
            tmp = tuple(map(lambda x:(ord(x)-ord(s[0]))%26, s))
            dic[tmp] = dic.get(tmp, []) + [s]
        return [sorted(x) for x in dic.values()]

  • 0

    nice man~ I still need to work on my build-in function ha! thxs for sharing

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.