-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path249__medium__group-shifted-strings.js
58 lines (48 loc) · 1.13 KB
/
249__medium__group-shifted-strings.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence:
"abc" -> "bcd" -> ... -> "xyz"
Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.
Example:
Input: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"],
Output:
[
["abc","bcd","xyz"],
["az","ba"],
["acef"],
["a","z"]
]
*/
/**
* @param {string[]} strings
* @return {string[][]}
*/
/**
*
* O(nk) time (for n string, avg length k)
* O(nk) space
*
* 30m
*/
var groupStrings = function(strings) {
let map = {};
let res = [];
for (let i = 0; i < strings.length; i++) {
let k = getKey(strings[i]);
map[k] = map[k] || [];
map[k].push(strings[i]);
}
Object.values(map).forEach(group => {
res.push(group);
});
return res;
function getKey(str) {
let base = str[0].charCodeAt();
let key = "";
for (let i = 0; i < str.length; i++) {
let dist = str.charCodeAt(i) - base;
if (dist < 0) dist += 26;
key += dist + "|";
}
return key;
}
};