-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0249-group-shifted-strings.js
More file actions
37 lines (33 loc) · 1.31 KB
/
0249-group-shifted-strings.js
File metadata and controls
37 lines (33 loc) · 1.31 KB
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
31
32
33
34
35
36
37
/**
* Group Shifted Strings
* Time Complexity: O(N * L)
* Space Complexity: O(N * L)
*/
var groupStrings = function (inputStrings) {
const classifiedGroups = new Map();
const getShiftPattern = (processingString) => {
const stringLengthValue = processingString.length;
if (stringLengthValue === 1) {
return 'lengthOne';
}
const computedDiffs = [];
for (let charIterator = 1; charIterator < stringLengthValue; charIterator++) {
const currentAsciiValue = processingString.charCodeAt(charIterator);
const previousAsciiValue = processingString.charCodeAt(charIterator - 1);
let diffRaw = currentAsciiValue - previousAsciiValue;
const diffNormalized = (diffRaw + 26) % 26;
computedDiffs.push(diffNormalized);
}
return computedDiffs.join(',');
};
for (const currentInputItem of inputStrings) {
const stringPattern = getShiftPattern(currentInputItem);
if (!classifiedGroups.has(stringPattern)) {
classifiedGroups.set(stringPattern, []);
}
const existingGroupList = classifiedGroups.get(stringPattern);
existingGroupList.push(currentInputItem);
}
const finalResultArray = Array.from(classifiedGroups.values());
return finalResultArray;
};