-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0249-group-shifted-strings.py
More file actions
30 lines (25 loc) · 904 Bytes
/
0249-group-shifted-strings.py
File metadata and controls
30 lines (25 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# time complexity: O(n)
# space complexity: O(n)
from collections import defaultdict
from typing import List
class Solution:
def convertString(self, string: str):
temp = ""
diff = ord(string[0]) - ord('a')
for c in string:
alphet = ord(c) - diff
temp += chr(alphet + 26 if alphet < ord('a') else alphet)
return temp
def groupStrings(self, strings: List[str]) -> List[List[str]]:
groupDict = defaultdict(list)
for string in strings:
if string[0] == 'a':
groupDict[string].append(string)
else:
groupDict[self.convertString(string)].append(string)
result = []
for value in groupDict.values():
result.append(value)
return result
strings = ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"]
print(Solution().groupStrings(strings))