My simple Python solution with explanation

  • -1

    First iterate all strings, all strings is not empty here.
    In the loop, we convert string by moving character let it starts with 'a'. Use the converted string as key to group stings.

    In the end, output the stored value (grouped list).

        map = {}
        for s in strings:
            gap = ord(s[0]) - ord('a')
            group = map.setdefault(''.join([chr(ord('a') + (ord(i) - ord('a') - gap + 26) % 26) for i in s]), [])
        return [sorted(value) for key, value in map.iteritems()]

Log in to reply

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