[
string
hash table
]
Leetcode 0249. Group Shifted Strings
Problem statement
https://leetcode.com/problems/group-shifted-strings?/
Solution
For each string, find ooriginalo string, that one which start with a
(or any other fixed symbol). Keep it in defauldict(list).
Complexity
Both time and space complexity is $O(n)$, where $n$ is the sum of all lengths.
Code
class Solution:
def groupStrings(self, strings):
alph = "abcdefghijklmnopqrstuvwxyz"
d = defaultdict(list)
for s in strings:
q = "".join(alph[(ord(i)-ord(s[0])) % 26] for i in s)
d[q].append(s)
return d.values()