Leetcode # 249. Group Shifted Strings

https://leetcode.com/problems/group-shifted-strings

Solution

Time Complexity: O(len(strings))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def groupStrings(self, strings: List[str]) -> List[List[str]]:
    group = collections.defaultdict(list)
    for s in strings:
      key = []
      for i in range(1, len(s)):
        key.append((ord(s[i]) - ord(s[i - 1])) % 26)
      group[tuple(key)].append(s)
    return [group[key] for key in group]

 

Last Updated on 2023/08/16 by A1go

目錄

目錄
Bitnami