Skip to content

Latest commit

 

History

History
23 lines (20 loc) · 587 Bytes

249.md

File metadata and controls

23 lines (20 loc) · 587 Bytes
  • 36ms
  • 88%
class Solution:
    def groupStrings(self, strings: List[str]) -> List[List[str]]:
        hashTable = collections.defaultdict(list)
        for s in strings:
            key = self.getKey(s)
            hashTable[key].append(s)
        return [val for val in hashTable.values()]

    def getKey(self,s):
        # a-> 0, b = 1, z-> 25
        # z - a = 25
        # ( a - b  + 26 ) % 26 = 25
        res = ''
        for i in range(1, len(s)):
            gapStr = str((ord(s[i]) - ord(s[i-1]) + 26)%26)
            res += gapStr.zfill(2)
        return res